Php strtotime () "first monday of february" returns on second monday if february 1 is monday

I am working on a PHP function that computes holidays:

function holidays($country = 1, $timespan_start = 0, $timespan_end = 0) 

Holidays are returned as timestamps in the array.

Since I need to calculate dates like on the first Monday of February, I tried strtotime("first monday february $year") , and I found that this did not work in 2010, since 02/01/2010 - Monday - instead I I get February 8th.

This error is mentioned in the change log: In PHP 5 to 5.2.7, a request for a given occurrence of a given day of the week in a month when this day of the week was the first day of the month will incorrectly add one week to the returned timestamp. This has been fixed in 5.2.7 and later.

But I am using PHP 5.3.8. Why am I experiencing this error?

+7
source share
2 answers

Looks like you just missed the β€œfrom”:

 echo date('Ym-d', strtotime('first monday of february 2010')); 

will give the expected result . See the PHP Relativity Dates Guide for various input formats.

+11
source

Depending on your version of PHP, the 'of' statement may or may not work. As another solution try:

echo date('Ym-d',strtotime('monday February 2010'));

will return on the first Monday of February 2010. It works for all days.

+3
source

Source: https://habr.com/ru/post/899050/


All Articles