'all day' dates after the clocks change in march in GMT are stored in the database as 23:00 the day before. They display correctly in the default render, but not when i try and print them in a custom tpl. So this give the correct date:

print render($content['field_course_date']);
// output = "Friday, March 30, 2012"

This gives the day before:

print format_date(strtotime($node->field_course_date['und'][0]['value']), 'medium', '', 'Europe/London');
//output = "Thu, 29/03/2012 - 23:00"
//so does this
$date = new DateObject($node->field_course_date['und'][0]['value'], date_default_timezone(), DATE_FORMAT_ISO);
print $date->format('d') . ' ' . format('F');
//output = "29 March"

Can anyone help - i'm sure i'm missing something obvious here

Comments

amcc’s picture

Version: 7.x-2.0-rc2 » 7.x-2.2

i'm still struggling with this, i thought i'd fixed it but now some dates are appearing out (it does happen that they are near the BST (british summer time) date where the clocks move forward an hour. Heres the code i'm currently using in a Views template:

$timestamp = strtotime($row->_field_data['nid']['entity']->field_course_date['und'][0]['value']);
$output .= format_date($timestamp, 'custom', 'd'); //this prints one day behind the set date

All dates are appearing one day before the current date. Dates are set to "all day"

wusel’s picture

My tip on http://drupal.org/node/1477602 :

set "Time zone handling" to "No time zone conversion" for your date-fields.

mxh’s picture

#2: link leads to 404.

amcc’s picture

The link works if you take the colon off.

I don't think I can change the timezone handling now. It's a working site with lots of dates in there already.

The date fields if rendered on a node or view normally work absolutely fine with correct timezone handling. I'm looking to do some custom stuff and can't figure that out. I need the php function that the date module itself uses. In order to trigger the timezone handling correctly.

mxh’s picture

many times the database uses another timezone than your site configuration (for example UTC on the database and Europe/London as your default timezone in your site configuration.). If this is the case, you should first try to initialise a DateTime object with the timezone set up in your database, and then change the object's timezone.

Fortunately, date cck fields provide us information about the timezone from database (timezone_db) and the one set up by current user (timezone), so we can load it complete dynamically.

Example:

<?php
	
/* if you are in a node template file, usually you already have acces to $node object. otherwise get the id an load your node: */
/* $node = node_load($id); */
	
$item = field_get_items('node',$node,'field_course_date');
$item = $item[0];
	
$date = new DateObject($item['value'], new DateTimeZone($item['timezone_db']));
$date->setTimezone(new DateTimeZone($item['timezone']));
	
print date_format_date($date, 'custom', 'd.m.y H:i');

?>
wusel’s picture

My tip for new websites:
- Set the "Time zone handling" of ALL date-fields to "No time zone conversion".
- At http://drupal.org/node/1477602#note1 : "enter/import the event-time in the timezone of the location of the event.".
(like the flight-time on an airport, this is always local time/local timezone. The difference between the time of departure and arrival is different from the flighttime, if the timezones of the two airports are different)

amcc’s picture

maxelli - this is exactly what i was looking for, thank you.

I have a timezone set for the site already and also have a lot of data in the DB so cant really change that. Besides, I knew it was possible as the node displays the date fine, it was elsewhere i was struggling. This fixes everything for me!

For others who need help: I was customising a view, if you're doing that you can get the node id like so $node = node_load($row->nid); . Depending on what kind of view customisation you're doing, but debug() or print_r() will help you get the node id

mxh’s picture

amcc, you're welcome. for better data inspection, get familiar with the devel module. Especially the dpm() function could be a big help for you.

mxh’s picture

Status: Active » Closed (works as designed)
markfinney’s picture

#5 This is the first time I have actually understood why my dates were seemingly misbehaving!

Thank you so much for your clear example!!!