Php strtotime () various monday results this week

I have a problem with the date of Monday this week.

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

When I run the above code on my local machine (PHP 5.3), it correctly displays "2011-03-07", but the same code on my server (PHP 5.2) displays "2011-03-14" (which is Monday on next week).

I tried running the date ('W') on both machines and I get the same result (10).

Edit: Any ideas how to get this work right?

Thanks in advance.

+8
php strtotime
source share
7 answers

Not that I saw exactly this problem, but I saw very similar ones. strtotime seems to change behavior between different versions of PHP and prohibit simple operations (for example, "+1 week"), it is difficult to ensure that the functionality remains the same.

If you can't upgrade to 5.3, which, as correctly pointed out, seems to be an improvement, all I can recommend is playing with some permutations. Often you can get the right answer from older versions of strtotime by paraphrasing the question. Examples may include ...

  • next Monday -1 week
  • last Monday +1 week
  • this Monday
+4
source share

Using date('Ym-d',strtotime(date('o-\\WW')));

+8
source share

Yes, string interpretation in strtotime has improved significantly in 5.3.

+3
source share

Run the "date" command from the cli prompt on the server and on your local desktop. This will check if the OS sees the same date on both systems.

0
source share

You have two options: either do not use this check function, if the date is in the future, if so, then you will allow it for 7 days. Welcome to the wolderful world of PHP.

0
source share

Look at this:

"Keep in mind that you cannot rely solely on this feature to confirm a date, as it will accept crazy dates such as February 31st.

In addition, the "... week" functionality itself may not do what you expect. If it is used on Sunday, "next week" will not return the timestamp next Monday, but on Monday after that. Likewise, the timestamp for Monday of the current week is returned when “previous / last week” is used, and “this week” returns the timestamp on Monday of the next week (ie, Next Day). This is not the effect of the "week starts on Sunday," as it means that all returned timestamps must be on Sunday, and none of them will be. "

Ff tonight on Sunday, you're wrong.

0
source share

Here is another solution that works (actually the only thing that worked for me):

 $mondayOfWeek = date ('d M Y', strtotime($someDate) - ((date ('N', strtotime($someDate)) - 1) * 3600 * 24)); 
0
source share

All Articles