Php timestamp problems

Source: Internet
Author: User
Php timestamp question this post was last edited by the date function in the php function from 2013-03-0516: 20: 16 on hongming271 in some systems (such as & nbsp; Windows) from & nbsp; 1970 & nbsp; year & nbsp; 1 & nbsp; month & nbsp; 1 & nbsp; day to & nbsp; 2038 & nbsp; year & nbsp; 1 & nbsp; php timestamp

This post was last edited by hongming271 at 16:20:16

In php functions, the date function is limited in some systems (such as Windows) from January 1, January 1-20, 1970 to January 1, January 19.

Now I have a timestamp that exceeds 2038 and cannot be converted to the correct standard time format using date.

Another problem is that I have a timestamp of 1356969600, and the normal result is 00:00:00.
Example
$dt = new DateTime('@1356969600');
echo $dt->format('Y-m-d H:i:s');

The example is converted to 16:00:00, which is eight hours less than the normal effect.


------ Solution --------------------

Date_default_timezone_set ("Europe/London ");
$ Dt = new DateTime ('@ 1356969600 ');
Echo $ dt-> getTimezone ()-> getName (), "\ n ";
Echo $ dt-> format ('Y-m-d H: I: s'), "\ n ";
Echo date ('Y-m-d H: I: s ', 1356969600), "\ n ";


Date_default_timezone_set ("Asia/Shanghai ");
$ Dt = new DateTime ('@ 1356969600 ');
Echo $ dt-> getTimezone ()-> getName (), "\ n ";
Echo $ dt-> format ('Y-m-d H: I: s'), "\ n ";
Echo date ('Y-m-d H: I: s ', 1356969600), "\ n ";


Date_default_timezone_set ("Asia/Shanghai ");
$ Dt = new DateTime ('@ 1356969600 ');
Echo $ dt-> getTimezone ()-> getName (), "\ n ";
Echo $ dt-> format ('Y-m-d H: I: s'), "\ n ";
Echo date ('Y-m-d H: I: s ', 1356969600), "\ n ";


$ Dt-> setTimezone (new DateTimeZone ('Asia/Shanghai '));
Echo $ dt-> getTimezone ()-> getName (), "\ n ";
Echo $ dt-> format ('Y-m-d H: I: s'), "\ n ";
Echo date ('Y-m-d H: I: s ', 1356969600), "\ n ";

------ Solution --------------------

This post was last edited by xuzuning at 09:41:27 on

DateTime: format is designed not to be affected by external settings (this should be the case for object-oriented)
Therefore, no matter how you change the environment parameter date_default_timezone, the output results are not affected.

To change the time zone of DateTime: format
DateTime: setTimezone: input a time zone object DateTimeZone

For example
$d = new DateTime('@1356969600');
$d->setTimezone(new DateTimeZone('PRC'));
echo $d->format('Y-m-d H:i:s');
00:00:00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.