|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-02-02 16:42 UTC] t dot baumgarden at web dot de
Description:
------------
Today is 2015-02-02 (02.February)
I want first and last day ob last week:
$date = new DateTime('first day of last week');
echo $date->format('Y-m-d');
// ---
$date = new DateTime('last day of last week');
echo $date->format('Y-m-d');
result is (WRONG!):
2015-02-01
2015-02-28
MUST BE:
2015-01-26
2015-02-01
This working fine with "month" instead "week":
DateTime('first day of last month');
DateTime('last day of last month');
result is:
2015-01-01
2015-01-31
Test script:
---------------
$date = new DateTime('first day of last week');
echo $date->format('Y-m-d');
$date = new DateTime('last day of last week');
echo $date->format('Y-m-d');
Expected result:
----------------
2015-01-26
2015-02-01
Actual result:
--------------
2015-02-01
2015-02-28
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 17:00:01 2026 UTC |
According to the table "Day-based notations" on the manual page "Relative Formats"[1] "first day of" and "last day of" are always relative a *month*. What you're looking for is new DateTime('Monday of last week') resp. new DateTime('Sunday of last week') [1] <http://php.net/manual/en/datetime.formats.relative.php>