Forum Moderators: coopster
I can't get my date to properly display. It is $birthday. It is formatted as a time stamp. Previouslu, I had it in yyyy-m-d format. Neither worked! The output I get from the code below is Age: 33 (December 31, 1969).
WHY?
$today = time();
$age= ($today - $birthday)/60/60/24/365.25;
$f_age = sprintf("%01.0d", $age);
echo "Age: $f_age ";
$f_birthday = date("F d, Y", $birthday);
echo "($f_birthday)<br>";
[edited by: drollz at 3:50 am (utc) on Sep. 4, 2003]
You may need to convert $birthday into a UNIX timestamp before trying to subract it from $today(which is in UNIX format). mktime() [php.net] should do the trick. If not, see the Date and Time functions [php.net] for more options.
>Birdman
This works, EXCEPT for people born before 1969! People born before 1969 are all listed as "Age: 33 ()". Even a guy with a 1961-06-14
To clarify: The variable in which the birthday is stored in yyyy-mm-dd format is called $birthday. Here is my code:
php:
--------------------------------------------------------------------------------
$birthdate=strtotime($birthday);
$today = time();
$age= ($today - $birthdate)/60/60/24/365.25;
$f_age = sprintf("%01.0d", $age);
echo "Age: $f_age ";
$f_birthday = date("F d, Y", $birthdate);
echo "($f_birthday)<br>";
?>
--------------------------------------------------------------------------------
you may have to resort to digging out the year and subtracting and working from that angle... unix timestamps can only go so far... i've run into this problem in numerous ways over the years and generally always have had to resort to manually bashing things in to compliance... that means breaking everything out and doing it the hard way...
not sure if i can even offer any examples because much of my stuff is pascal code based when it comes to those problems... i do remember having some dbase 3 and dbase 4 code, too...
$todayplus = $today + 100000000000000000;
$birthdayplus = $birthdate + 100000000000000000;
and then $age = ($todayplus - $birthdayplus)/60, etc.
and it still came up with the same data! Why? I'm setting the date deep into the future, the whole 1970 timestamp origin should not be coming into play, should it?
// returns person's age in years
// accepts person's birthday in 'Y-m-d' formatfunction find_age($birthday){
list($byear, $bmonth, $bday) = explode('-', $birthday);
list($cyear, $cmonth, $cday) = explode('-', date('Y-m-d'));
$cday -= $bday;
$cmonth -= $bmonth;
$cyear -= $byear;
if($cday < 0)
$cmonth--;
if($cmonth < 0)
$cyear--;
return $cyear;
}