Deal with converting to correct timezone in php and drupal 7

<?php

// Title: Convert to correct timezone.

// Set default time.

$date = new DateTime('2018-05-17 22:29:00', new DateTimeZone('UTC'));

print $date->format('Y-m-d H:i:s') . "\n"; // 2018-05-17 22:29:00

// Now convert to correct timestamp.

$date->setTimezone(new DateTimeZone('Australia/Brisbane'));

print $date->format('Y-m-d H:i:s') . "\n"; // 2018-05-18 08:29:00

// ----------------------------------------------------------------

// The same as above but drupalway(using DateObject from date contrib module in drupal7).

$date = new DateObject('2018-05-17 22:29:00', new DateTimeZone('UTC'));

$date->setTimezone(new DateTimeZone('Australia/Brisbane'));

print $date->format('d/m/Y h:i:s'); // 2018-05-18 08:29:00

Nivel
Etiquetas