Date Display in Webform Emails
Posted on: Wednesday, September 30th 2009 by Steve Shen

In a recent project, we used the date component in webforms (http://drupal.org/project/webform) to create custom date-related fields. One problem we came across was that some of the dates were not shown correctly in the emails sent by the webform although they looked fine in web pages.

Checking the code of the webform module, we discovered that webform uses the function theme_webform_mail_date in the file components/date.inc to theme dates in emails. This theme function uses strtotime to convert a datetime string to Unix timestamp. The valid range of a timestamp is typically from Dec 13, 1901 20:45:54 UTC to Jan 19, 2038 03:14:07 UTC, which correspond to the minimum and maximum values for a 32-bit signed integer (http://us.php.net/manual/en/function.strtotime.php). If you're running a Unix/Linux box, you may check the range of dates supported on the system via the date command (eg. on my Ubuntu box, date -d '12/12/1901' renders invalid date while date -d '12/15/1901' displays Sun Dec 15 00:00:00 UTC 1901). Any datetime outside of the valid range won't be correctly displayed as the function strtotime returns false instead of a valid timestamp.

To fix this, we can create our own theme function to override the default one. Create the function phptemplate_webform_mail_date($data, $component) in template.php, and use the data variable to retrieve datetime information and display it the way we want without relying on the strtotime function.

Post new comment

The content of this field is kept private and will not be shown publicly.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.