Re: [请益] mktime()与时区问题

楼主: JoeHorn (每天都在公司玩OLG)   2016-07-19 02:51:03
※ 引述《st1009 (前端攻城师)》之铭言:
: 不才在下使用时间函式mktime()遇到问题,不太懂,希望可以得到各位先进解答>.<
: [略]
您遇到的问题,关键是 "日光节约时间"。
不知道为什么您会认为"错误",但我想下面这段程式可以帮您确认 PHP 的行为。
若是您的资料有跨时区存取/计算的考量,建议用 GMT/UTC 相关 functions。
<?php
echo "* Before tz setting" . PHP_EOL;
echo date_default_timezone_get() . PHP_EOL;
$the_time = mktime(1,1,1,8,17,1959);
$the_standard = mktime(1,1,1,7,10,2016);
echo '$the_time ='.$the_time. PHP_EOL;
echo '$the_standard = '.$the_standard . PHP_EOL;
echo date('Y-m-d H:i:s', $the_time) . PHP_EOL;
date_default_timezone_set('Asia/Taipei');
echo "* After tz setting" . PHP_EOL;
echo date_default_timezone_get() . PHP_EOL;
$the_time = mktime(1,1,1,8,17,1959);
$the_standard = mktime(1,1,1,7,10,2016);
echo '$the_time ='.$the_time. PHP_EOL
echo '$the_standard = '.$the_standard . PHP_EOL;
echo date('Y-m-d H:i:s', $the_time) . PHP_EOL;
/* 结果应该是这样:
* Before tz setting
Asia/Chongqing
$the_time =-327481139
$the_standard = 1468083661
1959-08-17 01:01:01
* After tz setting
Asia/Taipei
$the_time =-327484739
$the_standard = 1468083661
1959-08-17 01:01:01
作者: LPH66 (-6.2598534e+18f)   2016-07-19 03:28:00
http://tinyurl.com/jgoampv 台湾在 1959 确实有过日光节约8/17 是在这范围内所以才会差了一小时
作者: st1009 (前端攻城师)   2016-07-19 10:07:00
太感谢您了!!我没想到会受到日光节约影响 <3

Links booklink

Contact Us: admin [ a t ] ucptt.com