Date on e-mail sent is 1 day earlier.

JohnNoc - October 29, 2007 - 08:21
Project:Webform
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:critical
Assigned:Unassigned
Status:closed
Description

The date from the date field on the e-mail message I receive is 1 day earlier than the date the user chose and submitted in the form.
Eg. User chooses February 20 from the drop-down list, year 2008. Sends the form.
E-mail received- Submitted values are: Date: February 19, 2008

The date is correct on the table at http://example.com/node/nid/results/table though.

#1

compa - November 2, 2007 - 22:31

I have the same problem, has anyone pinpointed or created a patch for it?

Database and 'results' tab of the webform show the correct date, only the email sent is off by one day.
This is very disturbing as we use it for input on date of birth.

#2

JohnNoc - November 12, 2007 - 16:09

and we use this for restaurant booking....so it's a MAJOR problem for us :-(

#3

compa - November 14, 2007 - 20:48

I've been searching through the code but it all seems fine ... where does this come from? Anyone from the dev-team of this module know why?

#4

Sixcolored - November 18, 2007 - 09:53

Same here, also for a restaurant booking.
Is there a patch available yet??

#5

Sixcolored - November 22, 2007 - 09:47

Any updates yet? Please let us know :)

#6

joe-b - November 22, 2007 - 20:38
Status:active» patch (code needs review)

Is the problem that the function theme_webform_mail_date in components > date.inc has a format_date call that needs to be rewritten from this...

function theme_webform_mail_date($data, $component) {
  $output = $component['name'] .":";
  if ($data['month'] && $data['day']) {
    $timestamp = strtotime($data['month'] ."/". $data['day'] ."/". $data['year']);
    $output .= " ". format_date($timestamp, "custom", 'F j, Y', 0);
  }
  return $output;
}

to this ...

function theme_webform_mail_date($data, $component) {
  $output = $component['name'] .":";
  if ($data['month'] && $data['day']) {
    $timestamp = strtotime($data['month'] ."/". $data['day'] ."/". $data['year']);
    $output .= " ". format_date($timestamp, "custom", 'F j, Y', NULL);
  }
  return $output;
}

I believe from the API that format_date is expecting the last parameter to be a timestamp offset in seconds. If omitted, the user's time zone is used. In this case 0 would be interpreted differently from NULL. Seemed to work for me.

#7

compa - November 27, 2007 - 20:29

Didn't work for me ...

#8

Sixcolored - November 29, 2007 - 09:51

Same here, didn't work for me either....
I really need this to work correctly. Any updates so far?

#9

joe-b - December 4, 2007 - 16:45

Hmm, very unclear why this should be happening. The date is stored in the database as the three separate components (day, month, year) which you can see being dropped in on line 4 of the theme_webform_mail_date function...

function theme_webform_mail_date($data, $component) {
  $output = $component['name'] .":";
  if ($data['month'] && $data['day']) {
    $timestamp = strtotime($data['month'] ."/". $data['day'] ."/". $data['year']);
    $output .= " ". format_date($timestamp, "custom", 'F j, Y', 0);
  }
  return $output;
}

It's converted to a UNIX timestamp and then run through format_date to make it a nice readable string for the email. Couldn't be more simple.

I think the problem is something to do with the timezone in the format_date call. strtotime($data['month'] ."/". $data['day'] ."/". $data['year']) creates the timestamp at midnight (00:00 on month/day/year). If 0 is used for a timezone setting, then the timestamp will (I think) be GMT. If your timezone is forward of GMT, then the format_date call would work with a time behind your timezone, giving you the date for the previous day. Does that make sense...?

Hard to see what's going wrong without it being something related to the timezone setting, here and in the site settings. 0 is an integer/numeric and not the same as NULL.

Do you have a 'default value' set for your date component(s)? Does that make a difference?

The webform date component has a timezone option - that doesn't seem to be picked up in the theme_webform_mail_date function at the moment, so can't be anything to do with that.

Do you have user-configurable timezones in your Drupal installation? Is your site timezone set correctly?

I still think it should be

    $output .= " ". format_date($timestamp, "custom", 'F j, Y', NULL);

#10

nofue - January 5, 2008 - 15:11

Has anybody noticed that in the month/day selection pop ups to first entry is "month" and "day", respectively? When selected, it results in an invalid date error, but still not looking good to the untrainned eye.

Changing the output line as suggested in post 9 did the job here...

#11

compa - January 29, 2008 - 06:17

In the end it proved to be both for me ...

My site was set in GMT (instead of CET) _and_ the line had to be adjusted from 0 to NULL

tnx

#12

Rowanw - January 30, 2008 - 01:58

Changing 0 to NULL made no difference for me, it's still a day off.

#13

Rowanw - January 30, 2008 - 01:59
Status:patch (code needs review)» active

#14

Rowanw - January 30, 2008 - 02:01
Version:5.x-1.7» 5.x-1.x-dev

#15

brendanb - January 30, 2008 - 05:27

I suspect the output from strtotime() is affected by the timezone as PHP knows it, set on the server.
The format_date() function then applies Drupal-specific timezone on a timestamp that may not be GMT.

PHP here is set GMT+1000 with Drupal set the same. I think this caused date in email to be 20 hours behind.
Attached patch which works here. I'd be interested to know if this works for others in various timezones.

It's not great code, though best solution I could find without requiring PHP 5.1+

AttachmentSize
webform_date_email_offset.patch675 bytes

#16

Rowanw - January 30, 2008 - 06:52
Status:active» patch (code needs review)

Brendan's patch fixes my issue, which is no surprise considering we're working on the same project.

#17

Rowanw - February 4, 2008 - 03:19

Can we get some more testers on the latest patch?

#18

NicoWater - February 19, 2008 - 13:53

This code patch is also working ok for me.
Thank you

#19

mike-green - February 29, 2008 - 17:44

Works for me too.

#20

Rowanw - March 1, 2008 - 00:42
Status:patch (code needs review)» patch (reviewed & tested by the community)

Thanks guys.

#21

bigtrac - March 20, 2008 - 16:19

Didn't have any affect for me I'm afraid. Still the same problem of being 1 day behind.

#22

pebosi - April 15, 2008 - 08:29

Got the same error in Webform 2 beta 3, with "NULL" instead of "0" it worked for me...

regards pebosi

#23

quicksketch - April 15, 2008 - 09:22
Status:patch (reviewed & tested by the community)» fixed

Thanks everyone. I'm assuming that this fixes most sites, or at the very worst, doesn't cause harm to existing sites. Committed.

Anyone feeling ambitious can clean it up, but this has been going on so long I'd prefer just to leave it be and not touch it :)

#24

Anonymous (not verified) - April 29, 2008 - 09:27
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.