I'm trying to build my own output for a date field *see Group multiple times by day (multi-value date field) *, but the problem I'm having is how to apply the timezone?

This article -> Date value to different date format and nearly every other reference out there tells me that it should be applied by default. But it isn't.

What I'm doing is theming the field with:

function theme_field__field_times($vars){
    $dates = $vars["element"]["#values"];
    foreach($dates as $item){ // there are many dates, so we'll cycle through each one
        $date = ('U', strtotime($item['value'])); // convert to unix time
        print('This date renders as: '.format_date('$date', 'custom', 'D d/m/Y - g:ia', '', date_default_timezone_get());

Which outputs, for example, 27/07/2011 -1:10pm. The value should be 27/07/2011 - 11:10pm, or the date that my function spat out +10hours - the timezone ('Australia/NSW') that should have been applied...

date_default_timezone_get() is returning the correct timezone.

If I let Drupal render the dates using it's default field theming, the dates are right.

So why isn't the timezone being applied? How do I fix it?


The arguments you pass to the function are wrong; in particular, what you pass as last argument should be the one before it.
The arguments the function accepts are format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL), but your fourth argument is an empty string, and then you pass the timezone identifier as last argument, which should be the language identifier.
You are also using '$date' as first argument.

Try with the following call:

format_date($date, 'custom', 'D d/m/Y - g:ia', date_default_timezone_get()).
  • Thanks - fixed that but the time is still not being adjusted. I think, because the site is 'Australia/NSW', and the time as supplied in the 'value' doesn't have a timezone, it's assuming it is already Australia/NSW and not applying any adjustments. The array the date field returns looks like: [value] => 2011-07-27T23:10:00 [timezone] => Australia/NSW [timezone_db] => UTC [date_type] => date I would assume there's some pre-prepared function in the Date API for doing this, but I can't find it. Aug 4 '11 at 0:51
  • As far as I can see, it works when the server is using the UTC timezone; Drupal core modules normally use just three parameters, and the date is automatically converted basing on the Drupal site settings, though.
    – apaderno
    Aug 4 '11 at 1:51

I did something along the lines of this for a date field coming in as UTC that I wanted output using my site's timezone.

print format_date(strtotime($row->field_data_field_event_field_event_value.' '.$row->_field_data['nid']['entity']->field_event['und'][0]['timezone_db']));

When you convert the time-string to a timestamp using the strtotime function, it already applies the default timezone. Later, when calling format_date, the timezone is applied again - which breaks the calculation.

The solution is to add "UTC" to your time-string, which sets the "neutral" timezone. So you get a timestamp in UTC, which is then formatted and converted to the default timezone by format_date().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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