View Full Version : PHP TIME() and birthday
keyboard
09-01-2011, 03:27 AM
Is there a way to use time() on a website without relying on the user having the correct time set on their pc.
Also for a registration form, if I had three options Date Month Year for them to fill in their birthday how coan I work out how old they are and when it's their birthday, update the sql database with their new age?
djr33
09-01-2011, 03:50 AM
time() works on the server, and on the server only. It doesn't matter what time their OS claims it is. This is good, but it also can be a problem if they are in a different timezone. You can use some complicated methods to determine a timezone offset potentially if needed.
Birthdays are easy if you use timestamps. It's just some math to convert DD MM YYYY to a timestamp, then you can do any math you'd like, usually just by adding or subtracting a certain value, such as 60*60*24*365 (1 year). But there are other ways to manipulate timestamps using default functions. Look into this and you'll find your answers.
keyboard
09-01-2011, 07:54 AM
Could anyone please give me a good website which has information about timestamps?
JShor
09-01-2011, 01:14 PM
Birthdays are easy if you use timestamps. It's just some math to convert DD MM YYYY to a timestamp, then you can do any math you'd like, usually just by adding or subtracting a certain value, such as 60*60*24*365 (1 year).
You're forgetting about leap years. You'd need a conditional to determine how many leap year years have passed, add them together, multiply those days by 60*60*24 and subtract that value from the original equation.
You could attempt to convert a timestamp in DD MM YYYY format using strtotime(), which would give you a universal UNIX timestamp. You could also use strtotime() to subtract one year like strtotime("-1 year", <timestamp>);.
JShor
09-01-2011, 01:25 PM
I just realized how unhelpful my reply was to the original post.
Like Daniel said, time() is server-side only. It all depends on the server's time-keeping and the timezone in which the server is located.
Converting a birthday in MM DD YYYY is pretty simple. Let's say the user, through drop-down menus, have entered the following data:
Month: 11
Day: 17
Year: 1992
Well, then add the strings together and convert it using strtotime(). It will convert it to a UNIX timestamp. Then you can just do timestamp / (60*60*24*365). Note that it doesn't count leap year days, so it will probably be a few days off unless the user is 4 years old. That requires a separate algorithm.
For example:
<?php
$month = 11;
$day = 17;
$year = 1992;
$time = strtotime("$month $day $year");
$age = $time / (60*60*24*365);
echo "You are $age years old!";
?>
Would return "You are 18 years old!".
djr33
09-01-2011, 04:10 PM
You're right about the correction. I just skipped that to keep things basic. As I said, there are functions (such as strtotime()) that will help with doing more complicated mathematical operations and automating some of it.
Here's plenty of information (including a few tutorials). Time isn't easy to start with, but it's not very complicated once you understand it. Remember, the time is just stored as a number of seconds since 1970. So that is an easy way to deal with "time", since you're just counting seconds.
http://www.google.com/search?q=timestamps+in+php
keyboard
09-02-2011, 06:41 AM
I just realized how unhelpful my reply was to the original post.
Like Daniel said, time() is server-side only. It all depends on the server's time-keeping and the timezone in which the server is located.
Converting a birthday in MM DD YYYY is pretty simple. Let's say the user, through drop-down menus, have entered the following data:
Month: 11
Day: 17
Year: 1992
Well, then add the strings together and convert it using strtotime(). It will convert it to a UNIX timestamp. Then you can just do timestamp / (60*60*24*365). Note that it doesn't count leap year days, so it will probably be a few days off unless the user is 4 years old. That requires a separate algorithm.
For example:
<?php
$month = 11;
$day = 17;
$year = 1992;
$time = strtotime("$month $day $year");
$age = $time / (60*60*24*365);
echo "You are $age years old!";
?>
Would return "You are 18 years old!".
<?php
$month = 11;
$day = 17;
$year = 1992;
$time = strtotime("$month $day $year");
$age = $time / (60*60*24*365);
echo "You are $age years old!";
?>
I tried that and it just came up with
You are 0 years old!
Any help?
keyboard
09-02-2011, 06:48 AM
Also for a dropdown select so they can enter their age like this
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
Thats for the year column but how do I let them select a month and then based on that display how many days e.g
August - and it goes up to 31 days e.g
JShor
09-02-2011, 12:47 PM
Whoops, I forgot the slashes. I also forgot the floor(). Try this:
<?php
$month = 11;
$day = 17;
$year = 1992;
$date = "$month/$day/$year";
$time = strtotime($date);
$age = floor($time / (60*60*24*365));
echo "You are $age years old!";
?>
JShor
09-02-2011, 01:00 PM
As for the drop-down menus, it would just be easier to populate the HTML using loops.
<?php
$month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
echo " <select name=\"month\">";
foreach($month as $m) {
echo "<option value=\"$m\">$m</option>";
}
echo "</select>";
$d = 0;
echo " <select name=\"day\">";
while($d < 31) {
$d++;
echo "<option value=\"$d\">$d</option>";
}
echo "</select>";
$y = date("Y");
echo " <select name=\"year\">";
while($y >= date("Y", strtotime("-100 year"))) {
echo "<option value=\"$y\">$y</option>";
$y--;
}
echo "</select>";
?>
keyboard
09-03-2011, 09:55 PM
Yes!!! This is much easier, thanks JShor. Could you please do one more thing. If the select the month august, I would like the dropdown menu of days to be up to 31. However if they selected March then it would only go up to 30, because March only has 30 days in it etc...
Any help would be great!
JShor
09-04-2011, 11:43 AM
Hmm... Interesting problem. Well, this should work for you:
<script type="text/javascript">
var strHTML = "";
function chgDayCount(obj) {
if(obj.value == 'September' || obj.value == 'April' || obj.value == 'June' || obj.value == 'November') {
for(i=1; i<31; i++) {
strHTML += '<option value="' + i + '">' + i + '</option>';
}
document.getElementById('days').innerHTML = strHTML;
} else {
for(i=1; i<=31; i++) {
strHTML += '<option value="' + i + '">' + i + '</option>';
}
document.getElementById('days').innerHTML = strHTML;
}
}
</script>
Set the onchange event in the <select> menu with the name of month to chgDayCount(this) set the ID of the <select> named "day" to "days", and the ID of the <select> named "month" to "months". The code is pretty self-explanatory, I don't think I need to document it.
I think March has 31 days.
30 days has September, April, June and November.
JShor
09-04-2011, 11:49 AM
Oh, I didn't address the problem that it is likely (as it is with Firefox, Chrome, etc) that menu values are saved when the page is refreshed. So there poses the circumstance where the user will enter "November" and then refresh, and the option to select 31 as the day is possible.
You can solve this with a document ready check and just call the function with the object argument called by getElementById() and it will refresh the menu to have only 30 days (if in the case that September, April, June or November is selected).
If you're using jQuery, this is very easy to code:
<script type="text/javascript">
$(document).ready(function() {
chgDayCount(document.getElementById('months'));
});
</script>
If you're not... well... then you have a lot of coding to do.
keyboard
09-04-2011, 11:23 PM
Thanks for all your help JShor
<?php
$month = 11;
$day = 17;
$year = 1992;
$date = "$month/$day/$year";
$time = strtotime($date);
$age = floor($time / (60*60*24*365));
echo "You are $age years old!";
?>
<?php
$month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
echo " <select name=\"month\" onchange=\"chgDayCount(this)\" id=\"months\">";
foreach($month as $m) {
echo "<option value=\"$m\">$m</option>";
}
echo "</select>";
$d = 0;
echo " <select name=\"day\" id=\"days\">";
while($d < 31) {
$d++;
echo "<option value=\"$d\">$d</option>";
}
echo "</select>";
$y = date("Y");
echo " <select name=\"year\">";
while($y >= date("Y", strtotime("-100 year"))) {
echo "<option value=\"$y\">$y</option>";
$y--;
}
echo "</select>";
?>
<html>
<head>
<script type="text/javascript">
var strHTML = "";
function chgDayCount(obj) {
if(obj.value == 'September' || obj.value == 'April' || obj.value == 'June' || obj.value == 'November') {
for(i=1; i<31; i++) {
strHTML += '<option value="' + i + '">' + i + '</option>';
}
document.getElementById('days').innerHTML = strHTML;
} else {
for(i=1; i<=31; i++) {
strHTML += '<option value="' + i + '">' + i + '</option>';
}
document.getElementById('days').innerHTML = strHTML;
}
}
</script>
</head>
<body>
</body>
</html>
Is that what it should look like because when I select a month the days field goes blank???
I think March has 31 days.
30 days has September, April, June and November.
AWKWARD
Also I'm not using JQuery I once read about it but i have no clue about it. Could you give me a link to a page about it.
JShor
09-05-2011, 12:14 AM
It goes blank? What browser/version are you using? It works fine for me in FF 3.6+, IE, Opera and Chrome.
jQuery is easy to include. You just need to download the JS and include it in your code, and you can the programming interface to make writing JavaScript much faster.
http://jquery.org/
keyboard
09-05-2011, 12:22 AM
I'm using intetnet explorer 8
keyboard
09-05-2011, 12:46 AM
I just downloaded jQuery. How do I use it for what I'm trying to do?
JShor
09-05-2011, 01:42 AM
You're right, I see it in IE8 but not IE9. Looks like IE8- is the only browser with that problem. I'll look into it and let you know what I come up with.
keyboard
09-05-2011, 01:47 AM
Thanks JShor ,so much, for all your help!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.