0

I've got a date like : $date = DateTime::createFromFormat('D d/m', 'Mon 05/02'); but instead of 05 february the datetime returned is DateTime Object ( [date] => 2021-02-08 10:02:10.000000 [timezone_type] => 3 [timezone] => Europe/Brussels )

Answer Corrected with the Y input and got the right result, php was using 2021 when i was constructing 2022 year

  • February the 5th 2021 was a friday not a monday – schmauch Sep 28 '21 at 08:10
  • I'm guessing that probably has to do with the fact, that the 5th of February of this current year wasn't a Monday, but a Friday ...? With input value `Fri 05/02`, this gets you 2021-02-05 as to be expected. – CBroe Sep 28 '21 at 08:11
  • If you have input values like `Mon 05/02` stored somewhere, but you need to create the date for `05/02` of the current year - then remove the `D ` from your pattern, and cut the day name off the value. `DateTime::createFromFormat('d/m', explode(' ', 'Mon 05/02')[1])` – CBroe Sep 28 '21 at 08:14
  • Thanks guys/gals, i got the hint for the problem, i wasn't giving the year so php was using 2021, corrected with the Y input added and now it's correct – Stephane Desplanque Sep 28 '21 at 08:28

2 Answers2

1

If the (wrong) day of the week is to be ignored, then an * only needs to be set in the format instead of the "D".

$date = DateTime::createFromFormat('* d/m', 'Mon 05/02');

"Mon" is ignored and the expression "05/02" is used to determine the date.

DateTime::__set_state(array(
   'date' => "2021-02-05 18:28:31.000000",
   'timezone_type' => 3,
   'timezone' => "Europe/Berlin",
))
jspit
  • 7,276
  • 1
  • 9
  • 17
0

Because in 2021, February 5 is Friday, and February 8 is Monday.

Pocifik
  • 83
  • 6
  • 1
    Ok i think i got the wrong example (i was testing a manual date instead of my array) the dates for example is Mon 03/01 and it gave me 04/01 instead, but your response gave me an hint on the problem, i'm constructing a calendar for exams and the Mon 03/01 is for 2022 and since i don't gave him the year, he got the interpretation of 2021 ! Thank you – Stephane Desplanque Sep 28 '21 at 08:23