2007年1月3日 星期三

PHP5 時區設定

PHP5 timezone setting

PHP 5.1.0 開始
date() 會依據 date_default_timezone_get(void) 的設定去抓時區
如果沒有設定,就會依照格林威治標準時間
所以會跟我們差上 8 個小時(in Taiwan)

要在最上層加上:
date_default_timezone_set('Asia/Taipei');
就會正常了!

備註. SQL 的 now() 等日期時間函數不受此限制
在 PHP 5.1.0 以下的版本千萬不要加
要不然會馬上葛屁給你看喔 XD
Reference:
http://tw.php.net/manual/en/function.date-default-timezone-get.php
http://tw.php.net/manual/en/function.date-default-timezone-set.php
http://tw.php.net/manual/en/timezones.asia.फ्प

PHP5.1.X 取得系統時間戳記錯誤的原因

分類:PHP
2007/03/01 23:12
之前使用$_SERVER["REQUEST_TIME"]取得時間戳記時都會發現時區少了8小時
還得自行手動加上八小時
今天在線看官網的手冊時發現到從PHP5.1.X開始
設定檔中有個date.timezone屬性就是設定時區
http://www.php.net/manual/en/timezones.php
台灣就設成date.timezone = "Asia/Taipei"
這樣就ok了
PHP5.1.2開始額外提供一個Function
bool date_default_timezone_set ( string $timezone_identifier )
最主要讓script執行時能變更時區設定
date_default_timezone_set("Europe/Paris")
執行Script時時區就會改成Paris
很貼心的一個設計
另外一個小地方
很多人都使用time()來取得時間戳記
用$_SERVER["REQUEST_TIME"]會比time()來的快

【下列文章您可能也有興趣】

沒有留言: