开发者

Converting Unix timestamp to Locale timezone

开发者 https://www.devze.com 2023-03-03 20:37 出处:网络
All, How can I convert to a unix timestamp to date and time in local timezone. For timestamp = 1303374724716, the date (\'r\', $timestamp) function of PHP gives me Sun, 16 May 2032 22:11:37 +0000 whe

All,

How can I convert to a unix timestamp to date and time in local timezone. For timestamp = 1303374724716, the date ('r', $timestamp) function of PHP gives me Sun, 16 May 2032 22:11:37 +0000 whereas the epoch converter CORRECTLY converts to

GMT: Thu, 21 Apr 2011 08:32:04 GMT

Your timezone: Thu Apr 21 2011 04:32:04 GMT-0400 (Eastern Daylight Time)

I have seen the php.ini file and the default timezone is UTC. I don't understand why the value doesn't even match the GMT/UTC time. Can开发者_运维技巧 anyone please help me convert to my local timezone i.e. New_York.


That is because the timestamp you are given is in milliseconds.

Do:

$timestamp = floor(1303374724716/1000);

And everything will work as expected.


The docs say:

timestamp is optional and defaults to the value of time().

If you run

print time();

you will get a number like 1304640077.

Note that it is 1000 times smaller than the number you are trying to pass it.

In other words, you should be passing it a value in seconds, not milliseconds.

To set the time zone, use date_default_timezone_set, e.g.

date_default_timezone_set('America/New_York');

or set date.timezone in the php.ini file.

0

精彩评论

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