Ryan H
Ryan H

Reputation: 2953

date not convering to europe/london timezone in php

I'm working in a Cake PHP 2 web application where the default configuration is set to UTC. My model's created field I can see is saving in UTC (1 hour behind Europe/London, my current time).

When I retrieve my model in my view and output the contents of my created field it's the UTC time as saved in my db:

2022-08-12 15:31:05

But when trying to convert this to Europe/London using PHP datetime, it's not giving me the correct time of:

2022-08-12 16:31:05

The weird thing is that if I change my Europe/London in my DateTimeZone to UTC then it gives me:

2022-08-12 14:31:05

What am I missing to get the correct time:

$created = new DateTime($automatedReport['ConsolidatedReport']['created']);
$created = $created->setTimezone(new DateTimeZone('Europe/London'));
$created = $created->format('D jS, H:i:s');

Upvotes: 0

Views: 348

Answers (1)

Markus Zeller
Markus Zeller

Reputation: 9135

Create the date with UTC and then convert to London.

$created = new DateTime('2022-08-12 15:31:05', new DateTimeZone('UTC'));
$created = $created->setTimezone(new DateTimeZone('Europe/London'));
$created = $created->format('D jS, H:i:s');

gives

Fri 12th, 16:31:05

Upvotes: 1

Related Questions