简体   繁体   中英

Convert GMT date stamp to my timezone in PHP

I'm not sure what I'm doing is even correct. I'm being given a CSV export of transactions and the date stamp looks like a GMT timestamp to me, but when I try to convert it to my timezone and echo the time I just get the same timestamp.

$date = new DateTime('2019-11-12 13:43:12 +1300');
$date->setTimeZone(new DateTimeZone('Pacific/Auckland')); 
echo $date->format("Y-m-d H:i:s");

The Timezone "Pacific/Auckland" with Daylight Saving Time (DST) is 13 hours ahead of GMT so if you take a date time string with an offset of +13:00 and set the DateTimeZone to "Pacific/Auckland" you won't see any difference of the time because the offset will be the same. You will only see a difference if you either have a UTC (ending with Z ) or GMT date time string (ending with +00:00 ) and converting this to your time zone:

$date = new DateTime('2019-11-12 13:43:12+00:00');
$date->setTimeZone(new DateTimeZone('Pacific/Auckland'));
echo $date->format("Y-m-d H:i:sP");

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