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

4
  • February the 5th 2021 was a friday not a monday Commented Sep 28, 2021 at 8: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. Commented Sep 28, 2021 at 8: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]) Commented Sep 28, 2021 at 8: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 Commented Sep 28, 2021 at 8:28

2 Answers 2

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",
))
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain what is the timezone_type option please ?
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.