arc
arc

Reputation: 4691

Why does PHP 8 return the wrong second Friday of the next Year

To get the second Friday of next year I used second Friday of Januar next year, however, this returns the wrong date. (It's not only the second or Friday, this concerns every date in this fashion)

See following example


$FIRST_FRI_JAN_NEXT_YEAR_TEXT   = 'second friday of january next year';                        
$jan1 = new DateTime($FIRST_FRI_JAN_NEXT_YEAR_TEXT );     // = 22-01-08  // wrong date

$FIRST_FRI_JAN_NEXT_YEAR_NUMBER = 'second friday of 2022-01';
$jan2 = new DateTime($FIRST_FRI_JAN_NEXT_YEAR_NUMBER);    // = 22-01-14  // right date

Online Demo

Is there a reason for that or is this a bug?

Upvotes: 0

Views: 67

Answers (1)

jspit
jspit

Reputation: 7703

It is not a bug. “Second Friday in January next year” is interpreted as “second Friday in January” of this year and “next year” as +1 year. With long expressions it is not always clear in which order they are processed. It is usually better to do this in several steps.

$jan2 = date_create('next year')->modify('second friday of january');  //2022-01-14

Upvotes: 2

Related Questions