setISODate work that way?
Asked Answered
G

2

2

I am testing these cases:

$date = new \DateTime();
echo($date->format('Y.m.d')) . PHP_EOL;

$date->setISODate(2018, 1, 1);
echo($date->format('Y.m.d')) . PHP_EOL;

$date->setISODate(2019, 1, 1);
echo($date->format('Y.m.d')) . PHP_EOL;

$date->setISODate(2020, 1, 1);
echo($date->format('Y.m.d')) . PHP_EOL;

Ouput:

2017.08.17
2018.01.01
2018.12.31
2019.12.30

I understood that it would show this:

2017.08.17
2018.01.01
2019.01.01
2020.01.01

Why does that happen?

Surely there is something I did not quite understand.

Garrison answered 17/8, 2017 at 22:13 Comment(3)
You know that the second argument is a week number, not the month, right?Layman
check the docs bro and the examples. php.net/manual/en/datetime.setisodate.phpConflagrant
Are you sure you want DateTime::setISODate()? Its second argument is the week number, not the month. From your expectations I think in fact you are looking for DateTime::setDate().Kopeck
L
4

The arguments are year, week, dayofweek. PHP's week starts on Monday, and week 1 of a year is the first week that has at least 4 days in January.

In 2018, the year starts on Monday, so week 1 starts on January 1, and that's day 1 of the week.

In 2019, the year starts on Tuesday, so week 1 starts on Monday, December 31, 2018, and that's day 1 of the week.

In 2020, the year starts on Wednesday, so week 1 starts on Monday, December 30, 2019, so that's day 1 of the week.

Layman answered 17/8, 2017 at 22:27 Comment(1)
thanks for you complet explication. I did not consider the beginning of the week that way.Garrison
M
1

there is another problem with setISODate() especially visible for 1st week of 2021 (1.1.2021 - 3.1.2021).

ISO says it is a week #53, but once you do setISODate(2021, 53) you get year 2022

Monogenesis answered 25/1, 2021 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.