Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this weird behaviour in PHP strtotime

Tags:

php

timestamp

I am working on a calendar function that should output the name of the week and the date where I have the weeknumber and year

The code works fine when the year is 2018, but as soon as it is 2019, something goes wrong

echo date("Y-m-d D", strtotime("monday 2018W37")); // outputs 2018-09-10 Mon
echo date("Y-m-d D", strtotime("monday 2019W37")); // outputs 2019-09-15 Sun

But if you check a valid calendar, a monday in week 37 2019 is of cause a monday and the date is 2019-09-09

Can someone explain this behaviour and maybe provide an alternative to strtotime.

like image 398
Sebastian Td Avatar asked Dec 06 '25 05:12

Sebastian Td


1 Answers

You should write it like this,

echo date("Y-m-d D", strtotime("2018W37")) . "\n"; // outputs 2018-09-10 Mon
echo date("Y-m-d D", strtotime("2019W37")) . "\n"; // outputs 2019-09-9 Mon

or

echo date("Y-m-d D", strtotime("2018W37-1")) . "\n"; // outputs 2018-09-10 Mon
echo date("Y-m-d D", strtotime("2019W37-1")) . "\n"; // outputs 2019-09-9 Mon
like image 200
LF00 Avatar answered Dec 08 '25 19:12

LF00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!