Dealing with GMT being spat out by a cURL/datetime in PHP

Soldato
Joined
27 Dec 2005
Posts
17,296
Location
Bristol
I'm building a page for myself to gather and display API data from my energy provider. All good so far, cURL working and json_decode looking good and now just need to format the data.

Problem is the datetimes given are in GMT. I've tried sticking...

Code:
date_default_timezone_set('Europe/London');


...at the top, and...

Code:
$arr = json_decode($response, true);
foreach ($arr['results'] as $row){
$datetime = $row['valid_from'];
$datetime = new DateTime($datetime, new DateTimeZone('GMT'));


...further down, but no difference. Even changing it to Europe/Berlin or something makes no difference. So what's the best practice way of changing the timezone of the array?

Their documentation simply states:

Datetimes

Some API end-points accept datetime strings as parameters. These should be passed in ISO 8601 format. Eg: "2018-05-17T16:00:00Z"

We strongly recommend that timezone information is included on all datetime parameters. If no timezone information is included, the “Europe/London” timezone will be assumed and results may vary between GMT and British Summer Time.
 
Soldato
Joined
3 Jun 2005
Posts
3,117
Location
The South
To parse ISO8601 date strings you can either use the DateTime class -
Code:
$date = DateTime::createFromFormat(DateTime::ISO8601, '2020-05-18T21:30:00Z');
$date->setTimeZone(new DateTimeZone('Europe/London'));
print $date->format('d-m-Y H:i:s T');


Or use procedural date functions -
Code:
date_default_timezone_set('Europe/London');
$date = strtotime('2020-05-18T21:30:00Z');
print date('d-m-Y H:i:s T', $date);


Sandbox - http://sandbox.onlinephpfunctions.com/code/10286f2130a847480f5d4f6a0b01264e77447d34
 
Back
Top Bottom