So I've trying all sorts of combinations to get a date from my database (using Wordpress) to display in British Summer Time and I cannot get anything to work.
Is there any simple solution that can take the date string and make sure that in Summer Time in the UK it's an hour on from UTC time?
$classJson = $class->info;
$classJsonAsArray = json_decode($classJson, TRUE);
$classStartDate = strtotime($class->periodStart);
$classStartTime = date('H:i',$classStartDate);
So currently $class->periodStart returns: 2022-04-06 08:30:00
The time of that event should be 9.30am
All I need it to do is display the correct time, as at the moment, on the front end it displays as 8.30am.
CodePudding user response:
DateTime
handles timezones quite well.
$dateStringInUtc = '2022-04-06 08:30:00';
$date = new DateTime($dateStringInUtc, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Europe/London'));
echo $date->format('Y-m-d H:i:s'); // will output 2022-04-06 09:30:00
CodePudding user response:
Working with DateTime and timezones like in the accepted answer is the better way.
But it also works with strtotime if the timezone is appended to the date string. Date then returns the local time for a timestamp.
$utcDate = '2022-04-06 08:30:00';
echo date('Y-m-d H:i:s',strtotime($utcDate.' UTC'));