note 17428 added to function.mktime
| From: | donspeaker at hotmail dot com | Date: | Sat, 08 Dec 2001 08:53:00 +0000 |
| Subject: | note 17428 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-22063@lists.php.net to get a copy of this message | ||
You can calculate age using the code below. This code accounts for
the UNIX Epoch and leap years. All you need to do is provide it a
date in the m/d/yyyy format.
<?php
$birthday = "7/4/1960"; //must be as m/d/yyyy
$bday = explode("/", $birthday); //parse
$b_mm = $bday[0]; //birthday month
$b_dd = $bday[1]; //birthday day
$b_yyyy = $bday[2]; //birthday year
//compare timestamps of mm/dd for birthday and today
$bday_mm_dd = mktime(0,0,0,$b_mm,$b_dd,0);
$today_mm_dd = mktime(0,0,0,date("m"),date("d"),0);
$age = date("Y", time()) - $b_yyyy;
if ($bday_mm_dd > $today_mm_dd) {
//birthday hasn't arrived yet, so subtract a year
$age = $age - 1;
}
print $age; //age equals 41 (on date of this note)
?>
Of course this code assumes the client and server machines exist in
the same day.
-DON
--
http://www.php.net/manual/en/function.mktime.php
http://master.php.net/manage/user-notes.php?action=edit+17428
http://master.php.net/manage/user-notes.php?action=delete+17428
http://master.php.net/manage/user-notes.php?action=reject+17428