Reputation: 68660
PHP:
$start_time = '2020-06-23T22:30:00Z';
$timezone = new DateTimeZone("Australia/Sydney");
$date1 = new DateTime($start_time, $timezone);
$result = $date1->format('Y-m-d H:i:s');
echo $result; // 2020-06-23 22:30:00
JS (matches what I have set):
var date1 = new Date('2020-06-23T22:30:00Z')
console.log(date1) // Wed Jun 24 2020 08:30:00 GMT+1000 (Australian Eastern Standard Time)
What am I doing wrong here?
Upvotes: 1
Views: 35
Reputation: 23011
Passing the timezone into the DateTime constructor will override the Z flag on your time string. Create the object without the timezone, then set it afterwards:
$date1 = new DateTime($start_time);
// object(DateTime)(
// 'date' => '2020-06-23 22:30:00.000000',
// 'timezone_type' => 2,
// 'timezone' => 'Z'
// )
$date1->setTimezone($timezone);
// object(DateTime)(
// 'date' => '2020-06-24 08:30:00.000000',
// 'timezone_type' => 3,
// 'timezone' => 'Australia/Sydney'
// )
Upvotes: 4