php / php-src

The PHP Interpreter
https://www.php.net
Other
38.2k stars 7.75k forks source link

DateTime and wrong ISO 8601 week numbers #16762

Closed Nekuin closed 1 day ago

Nekuin commented 1 day ago

Description

The following code:

<?php
        $dates = ["2024-12-28", "2024-12-29", "2024-12-30", "2024-12-31", "2025-01-01"];
        foreach ($dates as $date) {
            $d = new \DateTime($date);
            echo $date . " => " . $d->format("Y-W") . PHP_EOL;
        }

Resulted in this output:

2024-12-28 => 2024-52

2024-12-29 => 2024-52

2024-12-30 => 2024-01

2024-12-31 => 2024-01

2025-01-01 => 2025-01

But I expected this output instead:

2024-12-28 => 2024-52

2024-12-29 => 2024-52

2024-12-30 => 2025-01

2024-12-31 => 2025-01

2025-01-01 => 2025-01

2024-12-30 and 2024-12-31 week numbers seem to be wrong

PHP Version

PHP 8.3.13

Operating System

Ubuntu 22.04

Nekuin commented 1 day ago

ah I actually just figured out I'm supposed to use 'o' format here

https://www.php.net/manual/en/datetime.format.php ISO 8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.

closing this!