为什么php的日期和unix的日期不匹配?

发布于 2024-12-17 21:48:40 字数 313 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

任谁 2024-12-24 21:48:40

我通过 date cmd 将时间重新更改为纽约时间

date 命令不提供更改时区的方法。您可能将其设置为纽约时间,但无论它已经设置为哪个时区,最终服务器时间都会有六个小时的偏差(奇怪的是,如果是 UTC,则应该有五个小时)。

使用 date 命令设置时间之前,设置 TZ 环境变量export TZ=America/New_York。服务器时间不正确可能会导致严重问题。

i rechanged the time to NewYork time by date cmd

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.

阿楠 2024-12-24 21:48:40

您可以在应用程序中初始化时区。当你这样做时会发生什么?

date_default_timezone_set('America/New_York');
echo date("Y-m-d H:i:s");

You can initialize the timezone in your app. What happens when you do this?

date_default_timezone_set('America/New_York');
echo date("Y-m-d H:i:s");
—━☆沉默づ 2024-12-24 21:48:40

我在 php.ini 中更改了“date.timezone =”America/New_York”

看,这是不熟悉形式逻辑的人的常见错误。
让我用一个小例子向您展示:

假设您只是把一些钱放进口袋里。
我碰到你就问你口袋里有钱吗?
你说“是”。

但你不能肯定地说!
您只能确定您把它放在那里
但口袋里可能有一个洞,或者已经被小偷偷走了,或者你只是混淆了口袋。
所以,你必须先检查,然后再回答。

尽管它在现实世界中可以忽略不计,但在编程中这种行为变得至关重要!
您可以更改任何设置,但您必须验证您的操作是否有任何效果
可能存在 apache 重新加载问题、错误的 ini.file 或某些运行时设置覆盖它。

Tl:博士
始终验证您的设置、变量、数据 - 如果它确实包含预期值!
不要依赖于您在某处更改了它的事实。

i change in php.ini the "date.timezone = "America/New_York""

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.

沉睡月亮 2024-12-24 21:48:40

这听起来像是没有在计算机上正确设置时区的问题。

首先,要验证 PHP 是否使用正确的时区,请尝试以下操作来查看。

echo date("Y-m-d H:i:s e");

如果您在脚本中设置正确,则应该为您提供“美国/纽约”。

如果是这样,我会尝试此处讨论的过程进行更改您系统上的时间,看看是否有效。

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.

echo date("Y-m-d H:i:s e");

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.

仅一夜美梦 2024-12-24 21:48:40

我只是在这里猜测,但是你重新启动你的 apache 服务器了吗?

I'm just guessing here but do you restarted your apache server?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文