Edit report at https://bugs.php.net/bug.php?id=63740&edit=1

 ID:                 63740
 Updated by:         google...@php.net
 Reported by:        salmanarshad2000 at yahoo dot com
 Summary:            strtotime seems to use both sunday and monday as
                     start of week
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 PHP Version:        5.4.9
 Block user comment: N
 Private report:     N

 New Comment:

I've actually recently updated the documentation about strtotime in regard to 
this very 
behavior. See Bug #52143.

The problem is that prior to PHP 5.3.0 the relative time formats "this week", 
"next week", 
"previous week" were taken to mean a 7 day period relative to the current time. 
However, 
the behavior was changed in PHP 5.3.0 to be interpreted as "a week period of 
Monday 
through Sunday". This is noted in the documentation for strtotime in the 
changelog section 
since last week.

http://php.net/manual/en/function.strtotime.php#refsect1-function.strtotime-changelog

We can see this behavior more clearly from the following code...


var_dump(date("D Y-m-d", strtotime("this week", strtotime("2012-12-08"))));
string(14) "Mon 2012-12-03"

var_dump(date("D Y-m-d", strtotime("this week", strtotime("2012-12-09"))));
string(14) "Mon 2012-12-10"


Here what you'll notice is that "this week" always starts on a Monday. Now, 
when you want 
make that format relative to a particular day of the week, let's say Sunday...

var_dump(date("D Y-m-d", strtotime("Sunday this week", 
strtotime("2012-12-08"))));
string(14) "Sun 2012-12-09"

var_dump(date("D Y-m-d", strtotime("Sunday this week", 
strtotime("2012-12-09"))));
string(14) "Sun 2012-12-16"


What you should notice is that "this week" is first normalized to "Mon 
2012-12-03" and 
"Mon 2012-12-10", respectively. Then the day is moved up to the first "Sunday" 
of that 
week (i.e. +6 days on each week).

This might sounds a little confusing because if you are expecting the week to 
begin on a 
Sunday and end on a Saturday then you would assume that "Sunday this week" 
would mean 
"2012-12-09" where the date is "2012-12-09" and the day is a Sunday. But that's 
not the 
case. "this week" means Monday of the current week and then move up until the 
very next 
Sunday. So if today is Sunday, we don't get today's date when we try "Sunday 
this week".


Previous Comments:
------------------------------------------------------------------------
[2012-12-11 15:18:40] salmanarshad2000 at yahoo dot com

Description:
------------
Weeks start on Sunday or Monday. However, in this regard:

1) strtotime behavior is not documented.
2) strtotime produces inconsistent results when "this week" is used.

Sample dates from month of December 2012 used the the test script:

Mon 2012-12-03
Tue 2012-12-04
Wed 2012-12-05
Thu 2012-12-06
Fri 2012-12-07
Sat 2012-12-08
Sun 2012-12-09
Mon 2012-12-10
Tue 2012-12-11
Wed 2012-12-12
Thu 2012-12-13
Fri 2012-12-14
Sat 2012-12-15
Sun 2012-12-16


Test script:
---------------
// function strtotime called on Sun 2012-12-09
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-09"))); 
// Mon 2012-12-10
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-09"))); 
// Sun 2012-12-16

// function strtotime called on Mon 2012-12-10
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-10"))); 
// Mon 2012-12-10
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-10"))); 
// Sun 2012-12-16


Expected result:
----------------
If Sunday is start of the week then "sunday this week" be less than "monday 
this week":

// function strtotime called on Sun 2012-12-09
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-09"))); 
// Mon 2012-12-10
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-09"))); 
// Sun 2012-12-09

// function strtotime called on Mon 2012-12-10
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-10"))); 
// Mon 2012-12-10
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-10"))); 
// Sun 2012-12-09

If Monday is start of the week then "monday this week" should return different 
values on sunday and monday:

// function strtotime called on Sun 2012-12-09
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-09"))); 
// Mon 2012-12-03
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-09"))); 
// Sun 2012-12-09

// function strtotime called on Mon 2012-12-10
echo date("D Y-m-d", strtotime("monday this week", strtotime("2012-12-10"))); 
// Mon 2012-12-10
echo date("D Y-m-d", strtotime("sunday this week", strtotime("2012-12-10"))); 
// Sun 2012-12-16

Actual result:
--------------
See test script, actual result is present alongside each line.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63740&edit=1

Reply via email to