View Full Version : Dates before 1970
01-05-2007, 02:14 AM
I have been trying to do this all day, to no avail.
I am trying to take a YYYY-MM-DD date from a database and calculate the age of the person. It works fine until I try to do someone who was born before 1970.
Is it even possible to calculate someones age if they were born before 1970?
If so, how?
Appreciate your time
01-05-2007, 03:44 AM
01-05-2007, 03:56 AM
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).
I'm pleased that they found a work around for this. The downside is that its only available on versions 5.1.0+.
In otherwords, you cannot work with date functions if your version is older than this, and cannot support negative numbers. I believe this will lead you to your only alternative: manual calculations (or to move to a unix platform :D)
$birthday = '1955-06-13';
list($BYear, $BMonth, $BDay) = explode('-', $birthday);
$CYear = date('Y');
$CMonth = date('m');
$CDay = date('d');
$age = 0;
if (!empty($BYear) && $BYear < $CYear)
$age += $CYear - $BYear;
if (!empty($BMonth) && !empty($BDay))
$monthDiff = $CMonth - $BMonth;
$dayDiff = $CDay - $BDay;
if ($monthDiff < 0 || ($monthDiff == 0 && $dayDiff < 0))
$age -= 1;
That should work for ya. Though I have a feeling this is easier to do... where's marek and raaf when you need them, lol.
01-05-2007, 03:59 AM
Thanks guys for the input... I'll check it out ASAP
01-05-2007, 04:02 AM
The link posted for you *should* work on a windows platform as well. Its negative, but not out of bounds on a 32 bit signed integer. I'd recommend trying it using that method as opposed to mine if you can - mine currently doesn't account for any dls areas.
01-05-2007, 05:18 AM
The code from the link above doesn't seem to work on windows, as I get this error whenever I try to find the age of someone born before 1970:
Warning: mktime() [function.mktime]: Windows does not support negative values for this function in **PHP FILE** on line 49
So, I'm trying Fou-Lu's code now...
mine currently doesn't account for any dls areas.
What does that mean? DayLight Savings?
If so, how does that affect this script?
Pardon my ignorance and thanks for all your help
EDIT: Plugged Fou-Lu's code in, works like a charm!! (I'm still interested in finding out what you mean about 'doesn't account for any dls areas', though) :)
Thanks so much!
01-06-2007, 06:25 AM
NP, you guessed it correct.
By dls I refer to both the daylight savings and what I never thought of earlier, timezone offsets. The only difference it would create is being +/- 24 hours from the given time, meaning you may be off as much as one day. But it will be valid from the timezone stated in, which is probably GMT. Won't create an overly high difference, unless you are planning on using the concept in a calender that is accessed by different users, only then would you be interested in making alterations if you allow the users to state their timezone offsets and daylight savings times. Which is actually, easy to do.
01-06-2007, 06:39 AM
OK- Thanks for all your time!
I'm just putting my friends/contacts into a local DB and the script alerts me if its their b-day.. Therefore, I don't think DLS/timezones should be an issue.
01-06-2007, 06:44 AM
No problem mate.
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.