Re: [請益] mktime()與時區問題

看板PHP作者 (每天都在公司玩OLG)時間7年前 (2016/07/19 02:51), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串2/2 (看更多)
※ 引述《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 -- ▄▆▇▍ ▆ ▇ ▏▎▍▌▋▊▉ φkaishin ┼── █▏── ▁▁▍▁ ─────── JoeHorn┬┼ ▄ꈠ ▂▄▎ ▄ ◢▆ ▄▎▄▅ └┼ ┼┐ ◢▂◤ ◢ ▄▄▆ ┼┴ -▂▄-──-─┼ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.179.219 ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1468867868.A.A46.html

07/19 03:28, , 1F
http://tinyurl.com/jgoampv 台灣在 1959 確實有過日光節約
07/19 03:28, 1F

07/19 03:28, , 2F
8/17 是在這範圍內所以才會差了一小時
07/19 03:28, 2F

07/19 10:07, , 3F
太感謝您了!!我沒想到會受到日光節約影響 <3
07/19 10:07, 3F
文章代碼(AID): #1NZIKSf6 (PHP)
文章代碼(AID): #1NZIKSf6 (PHP)