1

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.

3 Answers 3

4

Yep, i was having similar problems, but in Drupal 6.

I'm surprised your $field_event_date[0]['value'] was in New York time. My understanding is that that value comes straight out of the database, and it's pretty standard to keep all times in the db as UTC times, and only use local times when displaying them (since local times go crazy a couple of times a year).

$field_event_date[0]['timezone_db'] will tell you for certain what timezone $field_event_date[0]['value'] is in.

And $field_event_date[0]['timezone'] will tell you what timezone it should be displayed in (according to your site's 'default timezone'). If you look at $field_event_date[0]['view'], it should be in the display timezone.

When i'm trying to display dates from the raw 'value', i do something like...

/** 
* @param
*     $date A CCK date field, like you'd get in your node templates,
*     e.g. $field_event_date[0]
*/
function get_display_date($date) {
    $date_convert = new DateTime($date['value'], new DateTimeZone($date['timezone_db']));
    $date_convert->setTimezone(new DateTimeZone($date['timezone']));
    return $date_convert->format('Y-m-d H:i:s');
}

I've yet to find any documentation which can confirm all this, so i may be totally wrong! But it works for me so far. Hopefully Drupal 7 isn't too different.

0
3

In addition to Chichilatte's answer: If you're using the date_api module (part of the date project) you can also do this:

$date_object = new DateObject($date['value'], new DateTimeZone($date['timezone_db']));
$date_object->setTimezone(new DateTimeZone($date['timezone']));
return date_format_date($date_object, 'custom', 'Y m d');
0

Working with a Drupal 6 site this helped me get a reliable solution to display the date with correct timezone

// Get the timezone offset
$offset = date('Z', strtotime($node->field_date[0][value]));
// Get the date from datebase using database timezone
$unixdate = strtotime($node->field_date[0]['value'].' '.$node->field_date[0]['timezone_db']);
// Apply timezone offset and format display datetime
$time = format_date($unixdate, 'custom', 'g:ia', $offset);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.