|
|
Dates |
|
|
This is a function that I wrote to get an age in years from a mysql style (yyyy-mm-dd)
date. Using php's date() function wouldn't work for me out of the box becaus the dates
to be stored in the database were mostly pre-1970, and so were represented as negative
seconds in relation to 1/1/1970 (the Unix Epoch). This was simpler for me.
MySQL has
date features that work from year 0, so that works fine too.
/*I have the echo's in the function just for debug uses */
function mysql_to_timestamp($bday) {
# This puts our sql date into an array:
$darray = explode('-', $bday); # 1965-08-28
# mktime with no args returns seconds since Epoch
$today = mktime();
echo "today: $today";
# now we convert sql date to mktime date
# (seconds from Epoch) and echo it
$age = mktime(0, 0, 0, $darray[1], $darray[2], $darray[0]);
echo "age: $age";
# Subtract bday from today into var $tmp, echo it to screen
$tmp = ($today - $age);
echo "today-age: $tmp";
# 86400 seconds in a day
$age = $tmp / 86400;
echo "div by 86400: $age";
# Divide the days by 365 and we have years of age,
# floor() deletes the stuff after the decimal. We don't
# wanna be 36.43 years old, right
$age = floor($age / 365);
echo "div by 365: $age";
# all that for this
return $age;
}
|
|
Thus spake the master programmer:
``When the program is being tested, it is too late to make design changes.''
|
|
|