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

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

问题描述

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

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('Ymd 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?

推荐答案

根据php.ini 指令 手册页,如果您使用的是 5.3 之前的版本,则默认情况下不设置时区.然后,如果您查看 的页面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天全站免登陆