Skip to content
Advertisement

PHP strtotime() different results of monday this week

I have a problem getting the date of monday in the current week.

echo date('Y-m-d',strtotime('monday this week'));

When I’m running the above code on my local machine (PHP 5.3) it outputs correctly ‘2011-03-07’, but the same code on my server (PHP 5.2) outputs ‘2011-03-14’ (that’s monday next week).

I’ve tried to run date(‘W’) on both machines and I get the same result (10).

Edit: Any ideas how get this work correctly?

Thanks in advance.

Advertisement

Answer

Not that I’ve seen exactly this problem, but I’ve seen ones very similar. strtotime seems to change behaviour between different versions of PHP, and barring the simple operations (eg. ‘+1 week’), it’s difficult to guarantee that functionality will remain the same.

If you’re not able to upgrade to 5.3, which as correctly pointed out does seem to be an improvement, all I can recommend, is playing with some permutations. It’s often possible to get the right answer out of older versions of strtotime by rephrasing the question. Examples might include …

  • next monday -1 week
  • last monday +1 week
  • this monday
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement