Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created June 13, 2018 05:05
Show Gist options
  • Save edutrul/030fc7f225446f5d3167a342c9e8dd79 to your computer and use it in GitHub Desktop.
Save edutrul/030fc7f225446f5d3167a342c9e8dd79 to your computer and use it in GitHub Desktop.
Deal with dates 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment