why does the date php and date unix does not match

2019-09-06 11:33发布

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

标签: unix date time php
5条回答
Summer. ? 凉城
2楼-- · 2019-09-06 11:51

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");
查看更多
劳资没心,怎么记你
3楼-- · 2019-09-06 11:59

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.

查看更多
我命由我不由天
4楼-- · 2019-09-06 12:00

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.

查看更多
祖国的老花朵
5楼-- · 2019-09-06 12:00

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

查看更多
Bombasti
6楼-- · 2019-09-06 12:07

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.

查看更多
登录 后发表回答