在shell上,我的服务器时间是(简单date的bash):Sun Aug 29 10:37:12 EDT 2010
当我运行代码时,php -r "echo date('Y-m-d H:i:s');"我得到:2010-08-29 10:37:10
但是当我执行一个PHP脚本时,echo date('Y-m-d H:i:s');我得到了不同的时间 - 因为php认为时区是UTC(服务器时间是EDT).
我的php.ini文件没有设置时区,我不想改变任何东西 - 因为我不知道它会对在这个服务器上运行的其他站点产生什么影响.
是否有一种简单的方法可以在为服务器设置的时区获得服务器时间?
Dan*_*uis 14
根据php.ini指令手册页,如果您使用的是5.3之前的版本,则默认情况下不会设置时区.然后,如果您查看页面date_default_timezone_get,它将使用以下顺序获取时区:
* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)
Run Code Online (Sandbox Code Playgroud)
如果所有这些都失败,则UTC是默认值,因此这可能是一个很好的起点.