note 17428 deleted from function.mktime by jimw
| From: | jimw@php.net | Date: | Thu, 20 Dec 2001 22:24:10 +0000 |
| Subject: | note 17428 deleted from function.mktime by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-23487@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