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]['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?
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.
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]['timezone']; date_default_timezone_set($event->field_event_date[$event->language]['timezone_db']); $date = strtotime($event->field_event_date[$event->language]['value']); $date_formatted = format_date($date, 'custom', 'M j, Y g:i a', $timezone); ?>