Re: [請益] mktime()與時區問題
※ 引述《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
07/19 03:28, 1F
→
07/19 03:28, , 2F
07/19 03:28, 2F
推
07/19 10:07, , 3F
07/19 10:07, 3F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):