PHP strtotime gives an invalid date for specific months

following code.

strtotime("first saturday", strtotime("+2 month")); 

It works correctly, but with the April months +2 month , October + 8 month , and December + 10 month gives the second Saturday this month is not the first.

Any ideas what causes it and how to stop it.

Wonderful

+4
source share
3 answers
 $saturday = strtotime("first saturday", strtotime("+2 month", strtotime(date("01-mY")))); 
+5
source

This is because the "first Saturday" is calculated from the specified date. If the set date is already Saturday, the next is calculated.

If you need the first Saturday from a specific month, follow these steps:

 $stamp = time(); $tm = localtime($stamp, TRUE); // +1 to account for the offset, +2 for "+2 month" $begin = mktime(0, 0, 0, $tm['tm_mon'] + 1 + 2, 1, 1900 + $tm['tm_year']); if (6 == $begin['tm_wday']) { // we already got the saturday $first_saturday = $stamp; } else { $first_saturday = strtotime('first saturday', $begin); } 
+1
source

Use the first day of the month to create a start date, and then just check the month / year:

 function firstSaturday($month, $year) { if (!is_numeric($month) && !is_numeric($year)) { return -1; } return strtotime('first saturday', mktime(0, 0, 0, $month, 1, $year)); } 
0
source

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


All Articles