php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23211 April First Week strtotime day off error
Submitted: 2003-04-14 15:44 UTC Modified: 2003-04-22 15:54 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: jmalan at ocurrance dot com Assigned:
Status: Not a bug Package: Calendar related
PHP Version: 4.3.1 OS: Linux 7
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jmalan at ocurrance dot com
New email:
PHP Version: OS:

 

 [2003-04-14 15:44 UTC] jmalan at ocurrance dot com
I have designed a scheduler for a call center which uses the work week of saturday through friday. If you use strtotime("last saturday") in the first week of april after the first saturday of april your strtotime will be off by one day. This happens in any year only during this time frame. To test it make sure you change your dates on your testing server. Quick Fix is $lastThursday = strtotime("last friday"). 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-14 15:54 UTC] mbarrus at elixirlabs dot com
We were having a similar issue.  stringtotime does not seem to take in considuration Daylight Savings.
 [2003-04-14 16:24 UTC] jmalan at ocurrance dot com
Found a better fix. Instead of using strtotime("last saturday"), try this: 

date("m/d/Y H:i:s",strtotime("-1 week saturday"));

The timechange didn't seem to have any affect to strtotime this way.
 [2003-04-22 01:30 UTC] rasmus@php.net
If you specify the timezone at the end of your strtotime string it should work just fine.
 [2003-04-22 10:10 UTC] jmalan at ocurrance dot com
Maybe that should be stated in the documentation in the strtotime area so developers know to add that. What would the syntax be if you were doing last saturday?
 [2003-04-22 11:17 UTC] rasmus@php.net
There is a link to the format docs on the strtotime manual page.  strtotime() is not a PHP thing, it is a standard library used all over the place.
 [2003-04-22 11:50 UTC] jmalan at ocurrance dot com
I looked at the GNU link on the strtotime page and it states:

1 year
1 year ago
3 years
2 days

In the examples for php it states:

1 day
1 week
next Thursday
last Monday


The syntax is completely different in PHP than GNU: Date Input Formats. Which brings me back to my original statement, there is a bug in PHP that interprets "last saturday" (saturday as an example, any day breaks) incorrectly when daylight savings time is in effect.
 [2003-04-22 11:52 UTC] jmalan at ocurrance dot com
I tried using a timezone and that did not make any difference.
 [2003-04-22 12:04 UTC] rasmus@php.net
How is the syntax completely different?  Those php examples are all covered by the GNU docs as well.  The whole point of the function is that it understands many different formats.

And if you specify the right timezone in works just fine, just as I said.  Try this:

$ts1 = mktime(12,0,0,4,5,2003);
echo date("l H:i:s m/d/y",$ts1);
echo "<br>\n";
$ts2 = strtotime("last saturday PST",$ts1);
echo "<br>\n";
echo date("l H:i:s m/d/y",$ts2);
$ts3 = strtotime("last saturday PDT",$ts1);
echo "<br>\n";
echo date("l H:i:s m/d/y",$ts3);

This outputs:
Saturday 12:00:00 04/05/03
Saturday 00:00:00 03/29/03
Friday 23:00:00 03/28/03

exactly as expected.  I see no bug here.  
 [2003-04-22 13:04 UTC] jmalan at ocurrance dot com
SWEEEEET!!!!!!!!! 

There we go, that was what I was missing. You rock man!
 [2003-04-22 13:17 UTC] jmalan at ocurrance dot com
Unfortunately that still does not answer my question. I know how to get around the problem but the problem still exists. date("m/d/Y H:i:s",strtotime("last saturday MST"))
returns 04/04/2003 23:00:00 if the time is after 2 AM sunday morning the 6th of april. Try it, set your calendar to the 7th of april 2003.
 [2003-04-22 15:54 UTC] rasmus@php.net
The bug database is not a support forum.  It is for reporting bugs.  There is nothing wrong with the strtotime() function.  It is giving you exactly what you are asking it for.  In this case last saturday in MST may not be the same day as last saturday in MDT since you switched from one to the other in between that.  A simple way around it is to just add a timezone fuzz factor if you don't know which timezone you are working with.  As in: strtotime("12:00:00 last saturday MST") and strtotime("12:00:00 last saturday MDT");  One will give you 11am, the other 12, but both will be Saturday.  If you still have trouble understanding timezones, please ask any further questions on the php-general mailing list.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Feb 18 19:01:28 2020 UTC