note 47079 added to function.mktime
| From: | phpmanual at localpin dot com | Date: | Tue, 02 Nov 2004 09:03:12 +0000 |
| Subject: | note 47079 added to function.mktime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79487@lists.php.net to get a copy of this message | ||
Daylight saving time! Grrrrrrrrrr! (And a minor "Grrr!" for PHP making this less
intuitive than it should be).
In brief, NEVER add number of seconds, like 60*60*24, but use strtotime("+1 day",
$myDate)).
Some of the 'simple' functions above for calculating dates will NOT work correctly when a
date includes daylight saving time. Shown below is the problem illustrated and a simple solution.
EXAMPLE 1: WRONG!
I had the following loop, which worked fine all year, until this weekend, when the clocks changed,
and it broke! The problem is daylight saving time:
// Loop through dates adding 1 days worth of seconds each time
$myDate = 1098914400 // October 28th
for ($myDate = $fromDate; $myDate <= $toDate; $myDate = $myDate + 60 * 60 * 24) {
$printable = strftime("%d-%m-%Y %H:%M:%S", $myDate);
echo $printable
}
"28-10-2004 00:00:00"
"29-10-2004 00:00:00"
"30-10-2004 00:00:00"
"31-10-2004 00:00:00"
"31-10-2004 23:00:00" (oops! 31st October repeats and time now 23:00!!)
"01-11-2004 23:00:00"
"02-11-2004 23:00:00"
EXAMPLE 2: CORRECT!
$myDate = 1098914400 // October 28th
for ($myDate = $fromDate; $myDate <= $toDate; $myDate = strtotime("+1 day", $myDate)) {
$printable = strftime("%d-%m-%Y %H:%M:%S", $myDate);
echo $printable
}
"28-10-2004 00:00:00"
"29-10-2004 00:00:00"
"30-10-2004 00:00:00"
"31-10-2004 00:00:00"
"01-11-2004 00:00:00"
"02-11-2004 00:00:00"
----
Manual Page -- http://www.php.net/manual/en/function.mktime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47079
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47079&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47079&report=yes
Search -- http://master.php.net/manage/user-notes.php