Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP strtotime() different results of monday this week

Tags:

php

strtotime

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.

like image 739
Stefan O. Avatar asked Mar 09 '11 15:03

Stefan O.


2 Answers

Use date('Y-m-d',strtotime(date('o-\\WW')));

like image 62
woodo Avatar answered Sep 20 '22 11:09

woodo


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
like image 45
Jeff Parker Avatar answered Sep 19 '22 11:09

Jeff Parker