简体   繁体   中英

PHP Date in past not match fit

First, I want to convert date to integer of PHP.

echo strtotime('08-04-2016 10:33:45 GMT+7');

and that's result: 1460086425 . But when I check again with code:

$timezone = +7;
echo gmdate("H:i:s | d-m-Y ",1460086425 + 3600*($timezone+date("I")));

and that's result: 11:33:45 | 08-04-2016 11:33:45 | 08-04-2016

Why different from 11:33:45 | 08-04-2016 11:33:45 | 08-04-2016 and 08-04-2016 10:33:45 , over one hour.

I wonder if your timezone settings are not correct. I had a similar problem recently, and it was caused by the Daylight Saving Time not being applied correctly. I imagine that if the server does not know where it is, then it cannot know when DST starts and ends.

To find out of if this applies to you, try echo ini_get('date.timezone'); in a script - what do you get? Be sure to do this either in a web context if your problem is in a web script, or on the console if your problem is on the console. This is because the PHP settings to control this are often set separately.

If you find that your setting does not give the correct city that you'd normally specify for your TZ, then change it in your php.ini file. For example, for me, I use this:

date.timezone = Europe/London

As far as I know (while checking timezones ) the areas at GMT+7 do not observe DST. Also, date uses the timezone set in date.timezone which could be correct for your location but is probably incorrect for any other location.

It's better to use DateTime and specify the correct DateTimeZone . That way the chances of getting the time zone offset (and whether or not DST is active) wrong are significantly reduced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM