1

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?

7
  • Is the PHP date in UTC ? Commented Jun 23, 2020 at 16:56
  • @RiggsFolly, the trailing Z flag does not indicate Zulu time, but that it's a UTC time string - reference Commented Jun 23, 2020 at 17:03
  • @Klaycon Correct, sorry! However if you put UTC time into js in a browser it will use the timezone in the browser to rearrange the date/time according to timezone Commented Jun 23, 2020 at 17:10
  • GMT+1000?? What? 1000 what? Commented Jun 23, 2020 at 17:24
  • @Klaycon— "Zulu", "Z" and "UTC" (and even GMT) are effectively all the same thing: zero offset. They don't tell you anything about the format of the string. Commented Jun 23, 2020 at 22:24

1 Answer 1

4

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'
// )
Sign up to request clarification or add additional context in comments.

1 Comment

smart. I've always wondered how I'd have to deal with dates if I ever lived anywhere other than GMT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.