[php-src] Issue #13267: DateTime setISODate() results in wrong timestamp?
| From: | Daijobou | Date: | Mon, 29 Jan 2024 09:51:25 +0000 |
| Subject: | [php-src] Issue #13267: DateTime setISODate() results in wrong timestamp? | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246355@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13267
Author: Daijobou
### Description
I call every Monday a cronjob at 00:01. I want known the year and week from last week.
```php
<?php
$dt = new DateTime('last week monday 00:00:01', new
DateTimeZone('Europe/Berlin'));
$year = (int)$dt->format('Y');
$week = (int)$dt->format('W');
// After this, I call a second script with parameters ($year and $week) few times for different
tasks
$dto = new DateTime();
$dto->setISODate($year, $week);
$fmt = new IntlDateFormatter('de_DE', IntlDateFormatter::NONE, IntlDateFormatter::NONE,
'Europe/Berlin');
$fmt->setPattern('LLL dd');
// Hint: $dto->getTimestamp() is the date a week ago, but with current time. So its always the
time its called.
$week_start = $fmt->format($dto->getTimestamp());
$dto->modify('+6 days'); // for sunday
$week_end = $fmt->format($dto->getTimestamp());
echo $week_start . ' - ' . $week_end;
```
Resulted in this output:
```
Jan 23 - Jan 29
```
But I expected this output instead, for Timezone 'Europe/Berlin':
```
Jan 22 - Jan 28
```
The parameters $year and $week are only determined once. With these two values, the same script is
called several times to create each time a graphic containing the date for the Monday and Sunday of
the previous week. The script is called by a cronjob 00:01 every Monday. The graphics were created
on the server at 00:01:23 and later. The script needs less than 1 second to create the graphics.
So I remove the images and call the script again ~9 o'clock and its shows me now the correct
values "Jan 22 - Jan 28".
Is this a bug or am I doing something wrong here?
Because its worked for many years, but since 2024 its startet with wrong dates (one date shifted).
03.01. wrong date for last week December.
08.01. again wrong date
**15.01. shows the correct date
22.01. shows the correct date**
29.01. again wrong date
So its strange.
### PHP Version
PHP 8.2.15
### Operating System
AlmaLinux 8.9