Unix-time problem
| From: | Nicklas af Ekenstam | Date: | Mon, 05 Mar 2001 03:04:29 +0000 |
| Subject: | Unix-time problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-42554@lists.php.net to get a copy of this message | ||
Hi!
I wrote this simple function to return the current date minus supplied number of years:
function get_birthdate($age_in_years) {
// get the current timestamp into an array
$timestamp = time();
$date_time_array = getdate($timestamp);
$hours = $date_time_array["hours"];
$minutes = $date_time_array["minutes"];
$seconds = $date_time_array["seconds"];
$month = $date_time_array["mon"];
$day = $date_time_array["mday"];
$year = $date_time_array["year"];
// use mktime to recreate the unix timestamp
// subtracting age_low and age_high from the years
$timestamp = mktime($hours, $minutes,$seconds ,$month, $day,$year - $age_in_years);
$birthdate = strftime("%d/%m/%Y", $timestamp);
return($birthdate);
}
You, obviously, call it like this: echo get_birthdate(1);
Which would return a datestring that looks like this: 05/03/2000
Works nice, but the problem occurs when I try to subtract more than 31 years which is naturall since, as far as UNIX is concerned, the world didn't exist then.
(Took me a while to figure this one out though.)
Any clues on how to fix this so that I can go beyond the past 31 years?
Sincerely,
Nicklas