[php-src] Issue #15184: date & strtotime unexpected behaviour
| From: | abstractador | Date: | Wed, 31 Jul 2024 22:13:52 +0000 |
| Subject: | [php-src] Issue #15184: date & strtotime unexpected behaviour | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247413@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15184
Author: abstractador
### Description
The following code:
```php
<?php
$files = [];
$months = 6;
for ($i = 0; $i < $months; $i++) {
$startDate = date('Y-m-01', strtotime("-$i months"));
$endDate = date('Y-m-t', strtotime("-$i months"));
$data = [
'startDate' => $startDate,
'endDate' => $endDate,
];
print_r($data);
}
```
Resulted in this output:
```
Array
(
[startDate] => 2024-07-01
[endDate] => 2024-07-31
)
Array
(
[startDate] => 2024-07-01
[endDate] => 2024-07-31
)
Array
(
[startDate] => 2024-05-01
[endDate] => 2024-05-31
)
Array
(
[startDate] => 2024-05-01
[endDate] => 2024-05-31
)
Array
(
[startDate] => 2024-03-01
[endDate] => 2024-03-31
)
Array
(
[startDate] => 2024-03-01
[endDate] => 2024-03-31
)
```
But I expected this output instead:
```
Array
(
[startDate] => 2024-07-01
[endDate] => 2024-07-31
)
Array
(
[startDate] => 2024-06-01
[endDate] => 2024-06-30
)
Array
(
[startDate] => 2024-05-01
[endDate] => 2024-05-31
)
Array
(
[startDate] => 2024-04-01
[endDate] => 2024-04-30
)
Array
(
[startDate] => 2024-03-01
[endDate] => 2024-03-31
)
Array
(
[startDate] => 2024-02-01
[endDate] => 2024-02-29
)
```
Might be useful
```php
<?php
/**
* Error
*/
echo "### Start With Error ###\n";
$files = [];
$months = 6;
for ($i = 0; $i < $months; $i++) {
$startDate = date('Y-m-01', strtotime("-$i months"));
$endDate = date('Y-m-t', strtotime("-$i months"));
$data = [
'startDate' => $startDate,
'endDate' => $endDate,
];
print_r($data);
}
echo "### END With Error ###\n";
/**
* Works
*/
echo "### Start Works ###\n";
$files = [];
$months = 6;
$currentTimestamp = strtotime(date('Y-m-01')); // Get the timestamp for the first day of
the current month
for ($i = 0; $i < $months; $i++) {
$targetTimestamp = strtotime("-$i months", $currentTimestamp);
$startDate = date('Y-m-01', $targetTimestamp);
$endDate = date('Y-m-t', $targetTimestamp);
$data = [
'startDate' => $startDate,
'endDate' => $endDate,
];
print_r($data);
}
echo "### END Works ###\n";
/**
* Output
*/
### Start With Error ###
// Array
// (
// [startDate] => 2024-07-01
// [endDate] => 2024-07-31
// )
// Array
// (
// [startDate] => 2024-07-01
// [endDate] => 2024-07-31
// )
// Array
// (
// [startDate] => 2024-05-01
// [endDate] => 2024-05-31
// )
// Array
// (
// [startDate] => 2024-05-01
// [endDate] => 2024-05-31
// )
// Array
// (
// [startDate] => 2024-03-01
// [endDate] => 2024-03-31
// )
// Array
// (
// [startDate] => 2024-03-01
// [endDate] => 2024-03-31
// )
// ### END With Error ###
// ### Start Works ###
// Array
// (
// [startDate] => 2024-07-01
// [endDate] => 2024-07-31
// )
// Array
// (
// [startDate] => 2024-06-01
// [endDate] => 2024-06-30
// )
// Array
// (
// [startDate] => 2024-05-01
// [endDate] => 2024-05-31
// )
// Array
// (
// [startDate] => 2024-04-01
// [endDate] => 2024-04-30
// )
// Array
// (
// [startDate] => 2024-03-01
// [endDate] => 2024-03-31
// )
// Array
// (
// [startDate] => 2024-02-01
// [endDate] => 2024-02-29
// )
// ### END Works ###
?>
```
### PHP Version
PHP 8.3.4 (cli) (built: Mar 20 2024 17:20:15) (NTS)
### Operating System
Debian GNU/Linux 11 (bullseye)