php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68973 DateTime('first day of last week') not working
Submitted: 2015-02-02 16:42 UTC Modified: 2015-02-03 07:38 UTC
From: t dot baumgarden at web dot de Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 5.5.21 OS: centOS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: t dot baumgarden at web dot de
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-02 21:41 UTC] cmbecker69 at gmx dot de
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>
 [2015-02-03 07:38 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2015-02-03 07:38 UTC] aharvey@php.net
What cmbecker69 said. :)
 [2015-02-04 04:21 UTC] a at b dot c dot de
Assuming a culture that reckons weeks as running from Monday to Sunday and not, say, Sunday to Saturday. :)
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 17:00:01 2026 UTC