Join 3,558 readers in helping fund MetaFilter (Hide)

Tags:

Problem with php's imap_search function regarding time zones.
March 11, 2013 4:00 PM   Subscribe

I'm trying to use the php imap_search function to collect all of the emails I receive from my business's fulfillment company on a given day. The problem is that I can't seem to get the SEARCH ON to return all of the emails in a day, instead it seems to return all of the emails in a day shifted a few hours.

Howdy,

The emails are all being sent from PA, I live in OH, and my hosting is based in CA.

I have a simple test script that looks like this:

date_default_timezone_set('America/Los_Angeles');
$mailbox = imap_open ("{SERVER}", "USERNAME", "PASSWORD");
$mail = imap_search($mailbox,'ON "10-Mar-2013"');
foreach($mail as $msg){
$this_header = imap_headerinfo($mailbox, $msg);
echo $this_header->Subject.' - '.$this_header->Date;
}
imap_close($mailbox);

But produces a result of:

New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 14:26:46 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 15:44:47 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 16:17:51 -0400
New Order in Your Spreadshop spreadshirt.com [us_US] - Sun, 10 Mar 2013 17:56:03 -0400
New Order in Your Spreadshop spreadshirt.com [us_US] - Sun, 10 Mar 2013 17:56:03 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 18:06:08 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 18:25:00 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 19:10:37 -0400
New order in your Spreadshirt shop [us_US] - Sun, 10 Mar 2013 20:28:26 -0400
New order in your Spreadshirt shop [us_US] - Mon, 11 Mar 2013 00:20:50 -0400
New order in your Spreadshirt shop [us_US] - Mon, 11 Mar 2013 00:36:45 -0400
New order in your Spreadshirt shop [us_US] - Mon, 11 Mar 2013 01:43:02 -0400
New order in your Spreadshirt shop [us_US] - Mon, 11 Mar 2013 02:27:43 -0400

Which, as you can see, returns both e-mails from the 10th and the first 3 hours of the 11th.

I tried changing date_default_timezone_set('America/New_York'); to date_default_timezone_set('America/Los_Angeles'); to match up with my server host's location but it has no effect.

Any ideas of how to get the search function to only return results from the one day I choose?

Thanks.
posted by Jezztek to Computers & Internet (6 answers total)
 
Eeeep, the above example should start with: date_default_timezone_set('America/New_York');
posted by Jezztek at 4:01 PM on March 11, 2013


Have you tried specifying the timezone in the search criteria?
posted by missmagenta at 4:05 PM on March 11, 2013


Hmmmmm, I can't seem to figure out how to enter a timezone as a part of the search that won't throw an error. I'll see if I can find a way to do so.
posted by Jezztek at 4:13 PM on March 11, 2013


Hmm. Google isn't yielding much and I don't have an IMAP server handy to play with, but any chance it's using UTC?

Bit of a hack, but you could try getting a broader list of mail and filtering them on your end using, say, strtotime($this_header->Date).
posted by brennen at 4:31 PM on March 11, 2013


Unfortunately, I think this is a limitation of the IMAP protocol's search function. Here's the relevant portion of the IMAP RFC:

ON <date>
Messages whose internal date (disregarding time and timezone)
is within the specified date.


And some clarifying text:

Clarify that date searches disregard the time and timezone of the
INTERNALDATE or Date: header. In other words, "ON 13-APR-2000" means
messages with an INTERNALDATE text which starts with "13-APR-2000",
even if timezone differential from the local timezone is sufficient
to move that INTERNALDATE into the previous or next day.


So you're limited to whatever timezone the server happened to store its internal dates in.
posted by zsazsa at 4:36 PM on March 11, 2013


Ok, so at least I'm not crazy!

I suppose it makes sense to grab the last two days worth at a time, then filter them on my end per brennen's suggestion.

Thanks much, all.
posted by Jezztek at 6:05 PM on March 11, 2013


« Older I'm a soon-to-be college gradu...   |  What are your strategies and b... Newer »
This thread is closed to new comments.