Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP set TimeZone without changing date?

Tags:

date

php

datetime

I send UNIX timestamp from javascript vat stamp = +new Date/1000 to PHP.

Then I do

//Here $d = '2015/04/03 00:00:00'
$d = new DateTime("@{$stamp}");
$d->setTimezone( new DateTimeZone( 'Pacific/Auckland' ) );

//Here $d = '2015/04/03 00:00:00' + 7:15 hrs ( 7:15 hrs is time diff between my browser & Auckland)

I want to change the timezone but keep the date to same. So, after I setTimezone to Pacific/Auckland, the date should still be '2015/04/03 00:00:00'.

like image 299
NestedWeb Avatar asked Apr 02 '15 05:04

NestedWeb


1 Answers

Here's one way to do it.

$_date = new \DateTime($date->format('Y-m-d H:i:s'), new \DateTimeZone('<time zone>'));
like image 68
Henry Avatar answered Oct 10 '22 09:10

Henry