[PHP]PHP timezone時區設置

摘要:[PHP]PHP timezone時區設置

最近在架設Web Server時,架設完PHP後,使用phpinfo()語法來去檢測PHP是否正常運作

在頁面觀察時,發現在date區塊出現錯誤訊息:

Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.


如圖:

 

之後去查看了php.ini檔,查詢到[Date]的;date.timezone =,是未被設置的

把前頭的「;」註解拿掉,並修改成date.timezone = "Asia/Taipei"

接著再重新啟動Apache Web Server即可
 



補充:
在未經修改前,使用date()函數也會出現錯誤訊息:


Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

如圖:



假如不想更改php.ini檔案,或需顯示其餘國家的時間,可以在程式碼內部設定。

經過實做,如下內碼:

<?php 
  date_default_timezone_set("Asia/Taipei");
  echo date("H:m:s");
?>

(參考資料:http://www.w3schools.com/php/php_date.asp)