开发者

php date offset

I'm using php's date() function to print out a time, which is retrie开发者_C百科ved from mysql. The actual time that's printed out is always one hour off.

I'm calling

date('H:i, d M Y', 100000)

I'm expecting "03:46, 02 Jan 1970" but got "04:46, 02 Jan 1970" instead.

Why?


Set timezone before, using date_default_timezone_set() because date() function depends of timezone setting (from php.ini or set by code)

That returns date/time according to your current timezone.

Check your actual timezone using

echo date_default_timezone_get();

Set (change) it using

date_default_timezone_set('UTC'); # UTC is just an example

For complete list of supported timezones in PHP check this link.

Example:

date_default_timezone_set('UTC');
echo date('H:i, d M Y', 100000);

Output:

03:46, 02 Jan 1970

Your current timezone offset is +0100 and that's why you've got 04:46, 02 Jan 1970. Also don't forget that back to 70's a lot of countries did not use DST rules.

References

  • date() - Format a local time/date
  • date_default_timezone_set() - Sets the default timezone
  • date_default_timezone_get() - Gets the default timezone
  • List of supported timezones


Looks like you're having a timezone issue.

You can set the timezone for your PHP app using date_default_timezone_set('UTC');, replacing UTC to whatever you require. This should fix your problem.

For more info, see: http://php.net/manual/en/function.date.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜