为什么php的日期和unix的日期不匹配?
我有 centos 服务器,我通过 date cmd 将时间重新更改为纽约时间。 我还在 php.ini 中更改了“date.timezone =”America/New_York”” 当我在命令 shell 中输入命令日期时。我知道纽约的实时时间。 但当我这样做时
回显日期(“Ymd H:i:s”);
时间不对啊! 我总是比纽约少 6 个小时。 请帮忙? 我已经尝试使用 date_default_timezone_set('America/New_York');在代码中没有任何帮助。 等待回复。 谢谢! 科伦·奥尔
I have centos server and i rechanged the time to NewYork time by date cmd.
Also i change in php.ini the "date.timezone = "America/New_York""
When i enter the command date in the command shell. i get the real time in new york.
But when I do
echo date("Y-m-d H:i:s");
The time isn't right!
I always get 6 hours less then the time in New York.
Please help?
I tried already using date_default_timezone_set('America/New_York'); in the code with no help.
waiting for a response.
Thanks!
Koren Or
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
date 命令不提供更改时区的方法。您可能将其设置为纽约时间,但无论它已经设置为哪个时区,最终服务器时间都会有六个小时的偏差(奇怪的是,如果是 UTC,则应该有五个小时)。
在使用 date 命令设置时间之前,设置 TZ 环境变量
export TZ=America/New_York
。服务器时间不正确可能会导致严重问题。The date command doesn't provide a means to change the timezone. You probably set it to the time that it is in new york time, but as whatever timezone it was already set to, so you ended up with the server time being six hours off (odd, it should be five off if it's UTC).
Set the TZ environment variable
export TZ=America/New_York
before setting the time with the date command. Having an incorrect server time can lead to serious problems.您可以在应用程序中初始化时区。当你这样做时会发生什么?
You can initialize the timezone in your app. What happens when you do this?
看,这是不熟悉形式逻辑的人的常见错误。
让我用一个小例子向您展示:
假设您只是把一些钱放进口袋里。
我碰到你就问你口袋里有钱吗?
你说“是”。
但你不能肯定地说!
您只能确定您把它放在那里。
但口袋里可能有一个洞,或者已经被小偷偷走了,或者你只是混淆了口袋。
所以,你必须先检查,然后再回答。
尽管它在现实世界中可以忽略不计,但在编程中这种行为变得至关重要!
您可以更改任何设置,但您必须验证您的操作是否有任何效果
可能存在 apache 重新加载问题、错误的 ini.file 或某些运行时设置覆盖它。
Tl:博士
始终验证您的设置、变量、数据 - 如果它确实包含预期值!
不要依赖于您在某处更改了它的事实。
Look, it is very common fault of people who aren't familiar with a formal logic.
Let me show you using a little example:
Suppose you just put some money into your pocket.
I come across and asks you if you got any money in your pocket.
You says "Yes".
But you can't say it for sure!
You can tell for sure only that you put it there.
But there can be a hole in the pocket or some thief picked it already or you just confused pockets.
So, you have to check it first, and answer later.
Although it is negligible in the real world, in programming such a behavior become critical!
You can change whatever settings but you have to verify, if your actions had any effect
There can be an apache reload issue, or wrong ini.file or some runtime setting overwriting it.
Tl:dr
Always verify your settings, variables, data - if it really contains expected value!
Do not rely on the fact you changed it somewhere.
这听起来像是没有在计算机上正确设置时区的问题。
首先,要验证 PHP 是否使用正确的时区,请尝试以下操作来查看。
如果您在脚本中设置正确,则应该为您提供“美国/纽约”。
如果是这样,我会尝试此处讨论的过程进行更改您系统上的时间,看看是否有效。
This sounds like an issue with not actually setting the timezone correctly on your machine.
First, to verify PHP is using the correct timezone, try the following to see.
That should give you "America/New York" if you have set it correctly in your script.
If so, I would try the procedure discussed here to change the time on your system and see if that works.
我只是在这里猜测,但是你重新启动你的 apache 服务器了吗?
I'm just guessing here but do you restarted your apache server?