0

Well,this seems strange. Please bear with me. Someone asked this question in SO. He wants the date of previous Monday. So i suggested

$monday=date(Y-m-d,strtotime('Monday this week'))

The output was perfect in my localhost. It showed 2012-07-30. Another guy commented that the function i mentioned isn't working. It is giving the same date like

$monday=date(Y-m-d,strtotime('Monday'))

i.e., 2012-08-06. And he isn't lying! The online editor which he linked is showing next monday's date. Check this! Why is this happening??

I searched, but couldn't get the reason behind it. Is it because of the older versions of php? Any help will be greatly appreciated. Thank you

Community
  • 1
  • 1
Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
  • 2
    Somewhere between 5.2.17 and 5.3.10 the problem was fixed: http://viper-7.com/1PPz5m (look at the paste history). The changelog for `strtotime()` doesn't mention this (surprise surprise) but I know that there *were* changes to `strtotime()` for 5.3 so I'm guessing this is what fixed it. – DaveRandom Aug 01 '12 at 10:59
  • OK, digging around in the [changelog for the 5.3.0 release](http://www.php.net/ChangeLog-5.php#5.3.0) I found this: `proper support for "this week", "previous week"/"last week" and "next week" phrases so that they actually mean the week and not a seven day period around the current day.` - sounds like that's probably the answer to me. – DaveRandom Aug 01 '12 at 11:05
  • I've used strtotime('Last Monday') before and it was ok through several versions of PHP but worth checking if there is a specific version issue – gunnx Aug 01 '12 at 11:05
  • @DaveRandom Yes i've seen it. I think it is same in the case of php 5.2.12. Am i wrong? – Bhuvan Rikka Aug 01 '12 at 11:11
  • Well I read into that message that it was broken before 5.3.0 - so in 5.2.12 it won't work right. Your version is giving you the right answer, ideone is giving you the wrong one because it's running a very old version. – DaveRandom Aug 01 '12 at 11:12

1 Answers1

1

Somewhere between 5.2.17 and 5.3.10 the problem was fixed: http://viper-7.com/1PPz5m (look at the paste history).

Digging around in the changelog for the 5.3.0 release I found this:

proper support for "this week", "previous week"/"last week" and "next week" phrases so that they actually mean the week and not a seven day period around the current day.

Sounds like that's probably the answer to me. Basically before 5.3.0 this week etc may give you the wrong answer, because it will look for the day in the 7 days surrounding the current date that is a Monday, whereas in 5.3.0 and later it will be interpreted correctly.

DaveRandom
  • 87,921
  • 11
  • 154
  • 174