note 72218 added to function.mktime
| From: | Stephen at osu1 dot php dot net | Date: | Mon, 08 Jan 2007 10:43:06 +0000 |
| Subject: | note 72218 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121421@lists.php.net to get a copy of this message | ||
There are several warnings here about using mktime() to determine a date difference because of
daylight savings time. However, nobody seems to have mentioned the other obvious problem, which is
leap years.
Leap years mean that any effort to use mktime() and time() to determine the age (positive or
negative) of some timestamp in years will be flawed. There are some years that are 366 days long,
therefore you cannot say that there is a set number of seconds per year.
Timestamps are good for determining *real* time, which is not the same thing as *human calendar*
time. The Gregorian calendar is only an approximation of real time, which is tweaked with daylight
savings time and leap years to make it conform more to humans' expectations of how time should
or ought to work. Timestamps are not tweaked and therefore are the only authoritative way of
recording in computers a proper order of succession of events, but they cannot be integrated with a
Gregorian system unless you take both leap years and DST into account. Otherwise, you may get the
wrong number of years when you are approaching a value of exactly X years.
As for PHP, you could still use timestamps as a way of determining age if you took into account not
only DST but also whether or not each year is a leap year and adjusted your calculations
accordingly. However, this could become messy and inefficient.
There is an alternative approach to calculating days given the day, month and year of the dates to
be compared. Compare the years first, and then compare the month and day - if the month and day have
already passed (or, if you like, if they match the current month and day), then add 1 to the total
for the years.
This solution works because it stays within the Gregorian system and doesn't venture into the
world of timestamps.
Here is a good discussion of this issue:
http://forums.devshed.com/php-development-5/
need-to-get-the-age-between-dob-
and-today-29925.html?&highlight=age+leap
[the above link was too long; combine the three lines to get the URL]
There is also the issue of leap seconds, but this will only arise if you literally need to get the
*exact* age in seconds. In that case, of course, you would also need to verify that your timestamps
are exactly correct and are not delayed by script processing time, plus you would need to determine
whether your system conforms to UTC, etc. I expect this will hardly be an issue for anybody using
PHP, however if you are interested there is an article on this issue on Wikipedia:
http://en.wikipedia.org/wiki/Leap_second
----
Server IP: 66.163.161.117
Probable Submitter: 68.163.130.212
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- https://master.php.net/note/edit/72218
Del: integrated -- https://master.php.net/note/delete/72218/integrated
Del: useless -- https://master.php.net/note/delete/72218/useless
Del: bad code -- https://master.php.net/note/delete/72218/bad+code
Del: spam -- https://master.php.net/note/delete/72218/spam
Del: non-english -- https://master.php.net/note/delete/72218/non-english
Del: in docs -- https://master.php.net/note/delete/72218/in+docs
Del: other reasons-- https://master.php.net/note/delete/72218
Reject -- https://master.php.net/note/reject/72218
Search -- https://master.php.net/manage/user-notes.php