php / php/php-src

strtotime() : 'first/last day of last/this/next X" gives unexpected results when X is either week or year

Offen
#19,684 2 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@derickr arbeitet bereits daran.

Seit 22.9.2025.

Extension: date Feature Status: Requires RFC
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

Description

The following code:

<?php 
$map = [
	'first day of last week'	=> '2025-08-25',	'last day of last week'	=> '2025-08-31',
	'first day of last month'	=> '2025-08-01',	'last day of last month'=> '2025-08-31',
	'first day of last year'	=> '2024-01-01',	'last day of last year'	=> '2024-12-31',
	
	'first day of this week'	=> '2025-09-01',	'last day of this week'	=> '2025-09-07',
	'first day of this month'	=> '2025-09-01',	'last day of this month'=> '2025-09-30',
	'first day of this year'	=> '2025-01-01',	'last day of this year'	=> '2025-12-31',
	
	'first day of next week'	=> '2025-09-08',	'last day of next week'	=> '2025-09-14',
	'first day of next month'	=> '2025-10-01',	'last day of next month'=> '2025-10-31',
	'first day of next year'	=> '2026-01-01',	'last day of next year'	=> '2026-12-31',
];

$time = strtotime($s = 'today');	
echo $s . "\t:\t" . date('Y-m-d H:i:s',$time) . PHP_EOL;
echo PHP_EOL;
echo "string\t\t\tresult" . PHP_EOL;
foreach ($map as $string => $date) {
	$time = strtotime($s = $string);
	echo $s . "\t" . ($d = date('Y-m-d',$time)) . "\t" . PHP_EOL; 
}
<?php

Resulted in this output:

today	:	2025-09-03 00:00:00

string			        result
first day of last week	2025-09-01	
last day of last week	2025-09-30	
first day of last month	2025-08-01	
last day of last month	2025-08-31	
first day of last year	2024-09-01	
last day of last year	2024-09-30	
first day of this week	2025-09-01	
last day of this week	2025-09-30	
first day of this month	2025-09-01	
last day of this month	2025-09-30	
first day of this year	2025-09-01	
last day of this year	2025-09-30	
first day of next week	2025-09-01	
last day of next week	2025-09-30	
first day of next month	2025-10-01	
last day of next month	2025-10-31	
first day of next year	2026-09-01	
last day of next year	2026-09-30

But I expected this output instead:

today	:	2025-09-03 00:00:00

string			        result
first day of last week	2025-08-25	
last day of last week	2025-08-31	
first day of last month	2025-08-01	
last day of last month	2025-08-31	
first day of last year	2024-01-01	
last day of last year	2024-12-31	
first day of this week	2025-09-01	
last day of this week	2025-09-07	
first day of this month	2025-09-01	
last day of this month	2025-09-30	
first day of this year	2025-01-01	
last day of this year	2025-12-31	
first day of next week	2025-09-08	
last day of next week	2025-09-14	
first day of next month	2025-10-01	
last day of next month	2025-10-31	
first day of next year	2026-01-01	
last day of next year	2026-12-31
PHP Version
PHP 8.4.10
Operating System

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.