I am having issues with dates and time zones in Drupal 7. I have created a content type that stores the date in $field_event_date
. When I use the Views module this works correctly. However, I am using a template file to manipulate the display of this data. When I output the raw value, it is correct, so is the time zone; when I attempt to format it with the time zone format_date()
seems to ignore the time zone value. Is there a proper way to do this in Drupal?
print '<br />' . $field_event_date[0]['value']; // outputs 2011-11-04 21:30:00
print '<br />' . $field_event_date[0]['timezone']; // outputs America/New_York
print '<br />' . date('g:ia', strtotime($field_event_date[0]['value'])); // outputs 9:30pm
print '<br />' . format_date(strtotime($field_event_date[0]['value']), 'medium', 'g:ia', $field_event_date[0]['timezone']); // outputs 9:30pm (????)
I have done this with just pure PHP this way:
// Get the timezone offset
$timezone = $field_event_date[0]['timezone'];
$remote_dtz = new DateTimeZone($timezone);
$remote_dt = new DateTime("now", $remote_dtz);
$offset = $remote_dtz->getOffset($remote_dt);
// Get the date
$start_date_string = $field_event_date[0]['value'];
$end_date_string = $field_event_date[0]['value2'];
// Apply the timezone offset
$start_date_string = strtotime(date("Y-m-d g:ia", strtotime($field_event_date[0]['value'])) . " " . $offset . " seconds");
$end_date_string = strtotime(date("Y-m-d g:ia", strtotime($field_event_date[0]['value2'])) . " " . $offset . " seconds");
// Build the date
$start_disp_date = date('l, F j, Y', $start_date_string);
$end_disp_date = date('l, F j, Y', $end_date_string);
$start_disp_time = date('g:ia', $start_date_string);
$end_disp_time = date('g:ia', $end_date_string);
This was working fine until recently a few dates started showing up about one hour off. I would prefer to use a Drupal centric solution (like format_date) since it might help me avoid odd occurrences like this one. Any advice for either approach would be appreciated however. This is all on a production site, so I just need to get it working.
Thank you for your time.
EDIT/UPDATE:
My PHP only solution was broken because of daylight savings time. So I replaced that code with this:
// Get the timezone offset
$offset = date('Z', strtotime($field_event_date[0]['value']));
// Apply the timezone offset
$start_date_string = strtotime(date("Y-m-d g:ia", strtotime($field_event_date[0]['value'])) . " " . $offset . " seconds");
$end_date_string = strtotime(date("Y-m-d g:ia", strtotime($field_event_date[0]['value2'])) . " " . $offset . " seconds");
// Build the date
$start_disp_date = date('l, F j, Y', $start_date_string);
$end_disp_date = date('l, F j, Y', $end_date_string);
$start_disp_time = date('g:ia', $start_date_string);
$end_disp_time = date('g:ia', $end_date_string);
Granted, this doesn't solve the problem with format_date()
. While I'm going to stick with my PHP centric solution, others might have the same problem. Perhaps I am using the function incorrectly.