By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,642 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,642 IT Pros & Developers. It's quick & easy.

date() and/or strftime() broken?

P: n/a
the following script...

<?php

$times = array(
'2004-04-04 00:31:00-06',
'2004-04-04 01:00:00-06',
'2004-04-04 01:59:59-06',
'2004-04-04 02:00:00-06',
'2004-04-04 03:00:00-06',
);

for ($i = 0; $i < count($times); $i++)
{
$s = $times[$i];
$time_t = strtotime($s);
print "strtotime('$s') = $time_t\n";
print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
print "\n";
}

?>

generates this output:

strtotime('2004-04-04 00:31:00-06') = 1081060260
date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"

strtotime('2004-04-04 01:00:00-06') = 1081062000
date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"

strtotime('2004-04-04 01:59:59-06') = 1081065599
date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"

strtotime('2004-04-04 02:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"

strtotime('2004-04-04 03:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"

date() seems to be picking rather arbitrary timezones for output.
Secondly, as best as I can tell, the fourth output is just patently WRONG
(or I'm too dense to see what's going on, but I don't think so).

I often want to calculate a time_t value at 00:00:00 (midnight) for an
arbitrary input time_t. I had been doing this as:

$midnight = strtotime(date('Y-m-d', $time_t));

but given the behaviour above, I don't think this works reliably. :(
The machine this is running on happens to be in MDT right now
(offset -06) and running PHP version 4.1.0. Can someone help me figure out
what the hell is going on here (or a *reliable* way to get a time_t value
for midnight of input strings of the form strtotime() handles)?

Thanks in advance.
-ej
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Erik Johnson wrote:
the following script...

<?php

$times = array(
'2004-04-04 00:31:00-06',
'2004-04-04 01:00:00-06',
'2004-04-04 01:59:59-06',
As per information on http://www.timeanddate.com/

before switching over to MDT the timezone offset was -0700
'2004-04-04 02:00:00-06',
This particular time did not happen.
MDT started on the second after 2004-04-04 01:59:59 (local time -- MST)
which was 2004-04-04 03:00:00 (local time -- MDT)
'2004-04-04 03:00:00-06',
);

for ($i = 0; $i < count($times); $i++)
{
$s = $times[$i];
$time_t = strtotime($s);
print "strtotime('$s') = $time_t\n";
print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
print "\n";
}

?>

generates this output:

strtotime('2004-04-04 00:31:00-06') = 1081060260
date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"
Wrong input
strtotime('2004-04-04 01:00:00-06') = 1081062000
date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"
Wrong input
strtotime('2004-04-04 01:59:59-06') = 1081065599
date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"
Wrong input
strtotime('2004-04-04 02:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"
Wrong input
strtotime('2004-04-04 03:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"
OK :)
date() seems to be picking rather arbitrary timezones for output.
Secondly, as best as I can tell, the fourth output is just patently WRONG
(or I'm too dense to see what's going on, but I don't think so).
Confused by timezone changes (and their respective offset)? :)
I often want to calculate a time_t value at 00:00:00 (midnight) for an
arbitrary input time_t. I had been doing this as:

$midnight = strtotime(date('Y-m-d', $time_t));

but given the behaviour above, I don't think this works reliably. :(


Let's abolish DST !!!!!

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
"Pedro Graca" <he****@hotpop.com> wrote:
Confused by timezone changes (and their respective offset)? :)
Yes. That's it (DOH!). ;) I forgot this had happened - I happened to be
looking at date/time stuff anyway.
Was very stressed out yesterday with a number of other things - not thinking
clearly.
Thank you for your reply. :)

-ej

in message news:c4*************@ID-203069.news.uni-berlin.de... Erik Johnson wrote:
the following script...

<?php

$times = array(
'2004-04-04 00:31:00-06',
'2004-04-04 01:00:00-06',
'2004-04-04 01:59:59-06',


As per information on http://www.timeanddate.com/

before switching over to MDT the timezone offset was -0700
'2004-04-04 02:00:00-06',


This particular time did not happen.
MDT started on the second after 2004-04-04 01:59:59 (local time -- MST)
which was 2004-04-04 03:00:00 (local time -- MDT)
'2004-04-04 03:00:00-06',
);

for ($i = 0; $i < count($times); $i++)
{
$s = $times[$i];
$time_t = strtotime($s);
print "strtotime('$s') = $time_t\n";
print "date('r', $time_t) = \"" . date('r', $time_t) . "\"\n";
print "\n";
}

?>

generates this output:

strtotime('2004-04-04 00:31:00-06') = 1081060260
date('r', 1081060260) = "Sat, 3 Apr 2004 23:31:00 -0700"


Wrong input
strtotime('2004-04-04 01:00:00-06') = 1081062000
date('r', 1081062000) = "Sun, 4 Apr 2004 00:00:00 -0700"


Wrong input
strtotime('2004-04-04 01:59:59-06') = 1081065599
date('r', 1081065599) = "Sun, 4 Apr 2004 00:59:59 -0700"


Wrong input
strtotime('2004-04-04 02:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"


Wrong input
strtotime('2004-04-04 03:00:00-06') = 1081069200
date('r', 1081069200) = "Sun, 4 Apr 2004 03:00:00 -0600"


OK :)
date() seems to be picking rather arbitrary timezones for output.
Secondly, as best as I can tell, the fourth output is just patently WRONG (or I'm too dense to see what's going on, but I don't think so).


Confused by timezone changes (and their respective offset)? :)
I often want to calculate a time_t value at 00:00:00 (midnight) for an arbitrary input time_t. I had been doing this as:

$midnight = strtotime(date('Y-m-d', $time_t));

but given the behaviour above, I don't think this works reliably. :(


Let's abolish DST !!!!!

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.