Date Display in Webform Emails
Wed, Sep 30, 2009 by Steve
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