Forum Moderators: coopster

Message Too Old, No Replies

formatting dates

i'm going nuts!

         

drollz

2:45 am on Sep 4, 2003 (gmt 0)

10+ Year Member



Wow, I am extremely frustrated right now; hopefully you guys can help me out before I'm out of dishes and glasses to break and have to move onto harming pets.

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]

Birdman

3:01 am on Sep 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

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

drollz

3:52 am on Sep 4, 2003 (gmt 0)

10+ Year Member



Thank you VERY VERY much for the help, but I still can't get this to work!

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>";
?>

--------------------------------------------------------------------------------

wkitty42

5:36 am on Sep 4, 2003 (gmt 0)

10+ Year Member



ummm, remember... in most computer dates, 1969 is the earliest then can go... why? because that is when their epoch started... this is the same item (the epoch) that will cause problems in 2038 due to it being able to only count so high... in your case, you're trying to count below the epoch of 0 (zero)...

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...

drollz

5:50 pm on Sep 4, 2003 (gmt 0)

10+ Year Member



I tried doing this

$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?

coopster

8:05 pm on Sep 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you tried this yet? It's straight out of the User Contributed Notes in the manual [us3.php.net] as Birdman stated earlier:

// returns person's age in years
// accepts person's birthday in 'Y-m-d' format

function 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;
}