PHP Timestamp issues

Source: Internet
Author: User
Tags echo date
The date function in PHP functions is limited to some systems, such as Windows, from January 1, 1970 to January 19, 2038.

Now I have a time stamp that is more than 2038 years old and does not normally translate into the correct standard time format with date.

Another problem is that I have a time stamp 1356969600, which translates into a normal effect of 2013-1-1 00:00:00
Example
$dt = new DateTime (' @1356969600 '), Echo $dt->format (' y-m-d h:i:s ');

The example conversion turned out to be 2012-12-31 16:00:00, eight hours less than the normal effect.


Reply to discussion (solution)

Date_default_timezone_set ("PRC");
Let's try this sentence.

Date_default_timezone_set ("PRC");
Let's try this sentence.
This useless, this timestamp is not real-time update, is the past time, so useless, you can try

8 hours is because China is in district +8.
It's best to change timezone in php.ini.

The timestamp is the number of seconds starting January 1, 1970, regardless of the time zone.


Manual: http://www.php.net/manual/en/datetime.construct.php

//Using a UNIX Timestamp. Notice The result is in the UTC time zone.
$date = new DateTime (' @946684800 ');
Echo $date->format (' y-m-d h:i:sp '). "\ n";

!--? php

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";


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 ...

echo $dt->format (' y-m-d h:i:s '), "\ n"; 2012-12-31 16:00:00
Date (' y-m-d h:i:s ', 1356969600), "\ n"; 2013-01-01 00:00:00
Time zone settings, I configured in the configuration file, the PRC is configured

Datetime::format is designed to be unaffected by external settings (object oriented)
So no matter how you change the environment parameter date_default_timezone, it will not affect the output result

To change the Datetime::format time zone setting, you need to
Datetime::settimezone Pass in a time zone object Datetimezone

Like what

$d = new DateTime (' @1356969600 '), $d->settimezone (New Datetimezone (' PRC ')), Echo $d->format (' y-m-d h:i:s ');
2013-01-01 00:00:00

Time zone problem ...

Look at my last paragraph.


Reference 5 Floor Helloyou0 's reply:
Date_default_timezone_set ("Europe/london");
$dt = new DateTime (' @1356969600 ');
echo $dt->gettimezone ()->getname (), "\ n";
echo $dt->format (' y-m-d h:i:s '), ...

You $dt = new DateTime (' @1356969600 '), in the @1356969600 timeline generated when the time is set, the conversion should be unified, otherwise you will have problems such as, when generated by: Date_default_ Timezone_set ("PRC"), the conversion page must also be used: Date_default_timezone_set ("PRC");

  • Related Article

    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.