php - why the day not equal to 24 hours timezone set to los angeles? -
background: our server php set timezone america/los_angeles
at work, found piece of code
$a = 1457856000; $b = $a + 24*3600-1; var_dump(date('y-m-d h:i:s', $a)); var_dump(date('y-m-d h:i:s', $b));
which output:
string(19) "2016-03-13 00:00:00" string(19) "2016-03-14 00:59:59"
quite strange, when code follow:
date_default_timezone_set('etc/gmt+8'); $a = 1457856000; $b = $a + 24*3600-1; var_dump(date('y-m-d h:i:s', $a)); var_dump(date('y-m-d h:i:s', $b));
it output:
string(19) "2016-03-13 00:00:00" string(19) "2016-03-13 23:59:59"
could explain why happened?
take @ output of
<?php date_default_timezone_set ('america/los_angeles'); $a = 1457856000; echo date('y-m-d h:i:s', $a), "\r\n\r\n"; for($i=0; $i<25; $i++) { $b = $a + ($i*3600)-1; echo date('y-m-d h:i:s', $b), "\r\n"; }
and marvel @ weirdness of daylight saving time.
2016-03-13 00:59:59 2016-03-13 01:59:59 2016-03-13 03:59:59 <- , here why tired day long next sunday 2016-03-13 04:59:59
edit: "and i'm wondering if there's native php function or approach make convenient format date without worrying summer time adjustments?"
yes, there is, see http://docs.php.net/datetime
<?php date_default_timezone_set('america/los_angeles'); $a = new datetime('2016-03-13 00:00:00'); $b = datetimeimmutable::createfrommutable($a)->modify('1 day'); $step = new dateinterval('pt1h'); while( $a <= $b ) { echo $a->format('y-m-d h:i:s'), "\r\n"; $a->add($step); }
prints
2016-03-13 00:00:00 2016-03-13 01:00:00 2016-03-13 03:00:00 2016-03-13 04:00:00 2016-03-13 05:00:00 2016-03-13 06:00:00 2016-03-13 07:00:00 2016-03-13 08:00:00 2016-03-13 09:00:00 2016-03-13 10:00:00 2016-03-13 11:00:00 2016-03-13 12:00:00 2016-03-13 13:00:00 2016-03-13 14:00:00 2016-03-13 15:00:00 2016-03-13 16:00:00 2016-03-13 17:00:00 2016-03-13 18:00:00 2016-03-13 19:00:00 2016-03-13 20:00:00 2016-03-13 21:00:00 2016-03-13 22:00:00 2016-03-13 23:00:00 2016-03-14 00:00:00
and
date_default_timezone_set('america/los_angeles'); $a = new datetime('2016-03-13 00:00:00'); $a->modify('+1 day -1 second'); echo $a->format('y-m-d h:i:s'), "\r\n";
prints 2016-03-13 23:59:59
Comments
Post a Comment