0

I have installed the Date Module and added a date field to one of my content types, the widget I'm using is Popup Calendar. The problem I'm facing is that when I choose a date it seems to get stored and displayed correctly in the administration, but at the frontend the date is shown with two hours added to it.

For example if I set the fields value to 2014-12-12 10:00:00 its displayed as 2014-12-12 12:00:00 when I'm visiting a frontend page.

Like some of the comments have pointed out this might be because the frontend is displaying a visitors local time, but still I don't get why the time differance is two hours, when the differance between Findland and Sweden is only one hour.

I guess this has do do with some Regional settings or similar.

  • Are you using any time format to display time in front end. – Jayendra Kainthola Dec 10 '13 at 9:59
  • How do you mean? I'm just use node_load() in a template and then using print $node->field_time[$node->language][0]['value']; to display the content. – Cyclonecode Dec 10 '13 at 10:06
  • I mean setting date format for date from admin section. – Jayendra Kainthola Dec 10 '13 at 10:08
  • 1
    Looks like a typical timezone issue. Is your site's or user's timezone 2 hours off from GMT? – Mołot Dec 10 '13 at 10:10
  • @Mołot - Yes it is. It's set to Europe/Helsinki: <time> +0200 and the pre selected country is Finland. I'm using the site from Sweden. But I have also unchecked that user may change their timezone. – Cyclonecode Dec 10 '13 at 10:15
3

Maybe try this:

// Example: Assume we have a node with nid 1 which contains a single
// language-neutral date field named field_date.
$node = node_load(1);
$date = $node->field_date[LANGUAGE_NONE][0];

// Create a date object.
$date_object = new DateObject($date['value'], new DateTimeZone($date['timezone_db']));
// Convert from the database time zone to the user's time zone.
$date_object->setTimezone(new DateTimeZone($date['timezone']));

// Example: Print a formatted date.
print date_format_date($date_object, 'custom', 'Y m d');

Source: Convert a date field to the correct time zone

1

If you're printing the date yourself, you could try loading the date with the given timezone to see what's returned.

// Just for fun, load a node that you know has the date field.
$node = node_load($nid);

// Grab the date value and timezone.
$date_value = $node->field_time['und'][0]['value'];

// For testing, try both the field timezone, and the server default.
$date_timezone = $node->field_time['und'][0]['timezone'];
$default_timezone = variable_get('date_default_timezone', '');

// Produce the new date.
$date = new DateTime($date_value, new DateTimeZone($date_timezone));
echo $date->format('Y-m-d H:i') . '\n';

$date = new DateTime($date_value, new DateTimeZone($default_timezone));
echo $date->format('Y-m-d H:i');    

// If you use Devel, just dpm it and see what you get back.
dpm($date);
  • 1
    When I'm doing the above I get 2014-02-12 08:00:00+02:00 and in the backend the same fields shows 2014-02-12 10:00:00. Using date_format($date, 'd.m.Y H:i'); I receive 2014-02-12 08:00:00? – Cyclonecode Feb 11 '14 at 22:21
  • @KristerAndersson I updated my answer. Try using the field timezone as well as the site default timezone. (I'm not sure if they're both set or not). You could also try Drupal's date_default_timezone to see if it's based on the user? – kyletaylored Feb 11 '14 at 22:40

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.