2

I would like to send an email out to a user when a new 'event' content type is posted. In this email, I want to display the title of the event, the body, and the date itself. For the date, I would like to print the date in the timezone of the user receiving the email.

So, when I send the email, I first get the user's account and the event node:

<?php
  $account = user_load($uid);
  $event = node_load($nid);
?>

I want to first check if the user has a timezone set in the user's profile, and if not, default back to the timezone used (and stored) with the event itself. So, something like:

<?php
  $timezone = $account->timezone ? $account->timezone : $event->field_event_date[$event->language][0]['timezone'];
?>

But, at this point, I'm not sure exactly how to print the date in the node in the correct timezone. I'm thinking I might have to first create a new DateTime object, then convert it using date.module's date_format_date, or maybe convert the date to a timezone, calculate the offset, and use PHP's date() function or format_date() to display the end result, but this all seems overkill.

Is there a simpler way to print out a date from a date field attached to a node in a user's timezone?

[Edit:]

I'm trying to format the date using format_date() or date_format_date(), but format_date() requires a time stamp, which is difficult to create from the date field's output, and date_format_date() also requires proper timezone information when I create a date object, and I've found it difficult to account for the timezone supplied by the date field, and then translate that into the timezone for output.

[Edit 2:]

Here's the code I ultimately used:

<?php
  $event = node_load([nid]);
  // Build event date + timezone.
  $timezone = $account->timezone ? $account->timezone : $event->field_event_date[$event->language][0]['timezone'];
  date_default_timezone_set($event->field_event_date[$event->language][0]['timezone_db']);
  $date = strtotime($event->field_event_date[$event->language][0]['value']);
  $date_formatted = format_date($date, 'custom', 'M j, Y g:i a', $timezone);
?>
1

You just need to use format_date() and pass a string for the timezone in the $timezone parameter, which by default will use the value returned by date_default_timezone_get().

  • But, is there an easy way to convert the date information contained in the date field into a timestamp that I can pass to format_date(), or do I need to do that manually? – geerlingguy Aug 6 '11 at 22:27
  • I'm trying to do everything manually and get a timestamp, but I'm having trouble converting the value I get in the date field in the node object translated into a UTC timestamp value rather than the field's value, so I can then translate the timezone. – geerlingguy Aug 6 '11 at 22:50
2

The problem is the configuration of the date field itself. In its settings (field management->edit), you will find an option "Time zone handling". This option controls the way this field is parsed and its display. I will venture a guess that yours is set to "Site's time zone". This means, that the default timezone will be used to convert this datetime into UTC, and the same will be used to display it. If changed to "User's time zone" option, then user's timezone will be used instead, and the default one will be the fallback if none such is set. The latter is taken into account from the format_date() function, without your having to set the $timezone parameter explicitly.

In your case, since each event has a timezone of its own, I would also consider the second option "Date's time zone". In this case, custom date timezone is not taken into account by format_date(), thus needs to be explicitly set.

Read the complete documentation here

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.