在php中获取服务器时间-时区问题 [英] get server time in php - timezone issues

查看:255
本文介绍了在php中获取服务器时间-时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell上,我的服务器时间是(bash中简单的date): 周日2010年8月29日上午10:37:12

on shell, my server time is (simple date in 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

when I run the code php -r "echo date('Y-m-d H:i:s');" I get: 2010-08-29 10:37:10

但是当我执行具有echo date('Y-m-d H:i:s');的php脚本时,会得到不同的时间-因为php认为时区为UTC(服务器时间为EDT).

but when I execute a php script that has echo date('Y-m-d H:i:s'); I get a different time- since php thinks the timezone is UTC (server time is EDT).

我的php.ini文件没有设置时区,并且我不想更改任何内容-因为我不知道它会对在此服务器上运行的其他站点产生什么影响.

My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.

是否有一种简单的方法来获取服务器时间-在为服务器设置的时区?

Is there a simple way to get the server time- at the timezone that is set for the server?

推荐答案

根据date_default_timezone_get ,它使用以下顺序获取时区:

According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

* 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)

如果所有这些都失败,则默认为UTC,所以这可能是一个很好的起点.

UTC is the default if all of those fail, so that's probably a good starting point.

这篇关于在php中获取服务器时间-时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆