Grokking Drupal dates and time zones

Grokking Drupal dates and time zones

in

I've run into a surprising amount of trouble trying to get time zones working properly in Drupal via the Date module. For instance, the date in the database might not align with the displayed date in the node, and when I go to edit the date in CCK, it displays yet another date. It's gotten so bad that I've had nightmares where calendars are bludgeoning me with timestamps, complaining that I just don't understand them, why don't I understand...

So I finally decided to dig a little deeper, and it's actually some simple stuff that I can summarize in a few bullet points:

  1. The date module saves dates in UTC, which stands for Coordinated Universal Time. So, regardless of your server, location or personal preference, when a date CCK field stores the date, it will offset it to match UTC. That means that if you and I make a post at the same time, even if we're in different time zones the date in the database will be the same for both of us.
  2. The CCK date field widget and it's output on a node might show two different things if you don't set the time zone setting in the CCK field. I set mine to match my site's so that I only have one variable to change later.
  3. Depending on what you need to do, you might need to explicitly set your time zone in PHP. For example, in order to get the date() function to output the time in my set time zone, I had to use date_default_timezone_set('America/Boise'). I might be missing something here, or need to update the date module, but this works for now.


Discussion on this post:
Implied By Design Logo