46

I want to use strtotime("last Monday").

The thing is, if today IS MONDAY, what does it return? It seems to be returning the date for the monday of last week. How can I make it return today's date in that case?

alex
  • 479,566
  • 201
  • 878
  • 984
Nathan H
  • 48,033
  • 60
  • 165
  • 247

8 Answers8

147

If you read the manual, there is an great example that describes exactly what you want to do http://www.php.net/manual/en/datetime.formats.relative.php

strtotime('Monday this week');

Update: There appears to be a bug introduced in newer versions of PHP where this week returns the wrong week when ran on Sundays. You can vote on the bug here: https://bugs.php.net/bug.php?id=63740

Update 2: As of May 18th 2016, this has been fixed in PHP 5.6.22, PHP 7.0.7 and PHP 7.1-dev (and hopefully remains fixed in subsequent releases) as seen here: https://bugs.php.net/bug.php?id=63740#1463570467

jmlnik
  • 2,867
  • 2
  • 17
  • 20
  • 4
    I don't get why the other answer was accepted. For further readers, this solution is better, it does exactly what is being asked. – Jose Garrido Jan 21 '13 at 20:50
  • Very useful for e.g. Drupal and Views where you can only input a date string. – Jide Feb 04 '13 at 13:25
  • This is clever piece of code imho, but it may be not selected because it lacks 'generalization', which can be important for people coming from Google. – EralpB Aug 16 '13 at 01:46
  • 1
    @EralpB, there's nothing clever about it. It's core PHP, widely supported, bug free, and semantically correct! :) – jmlnik Aug 19 '13 at 10:23
  • 6
    @Jose Garrido. Perhaps because this answer was given 2 years later? – Angus Walker Sep 12 '13 at 16:26
  • If the day is Sunday, this will return the date of Monday of next week, rather than the Monday of the current week. – chap Apr 19 '15 at 07:58
  • @chap, turns out ```this week``` returns a Monday if run on a Sunday. It's a ridiculous bug: https://bugs.php.net/bug.php?id=63740 – jmlnik May 17 '15 at 09:39
  • 5.6.23, 7.0.8 Weeks always start on monday. Formerly, sunday would also be considered to start a week. – Kuofp Oct 24 '17 at 05:17
  • Good luck relying on "this week" outside the US of A. – Chuck Nov 02 '20 at 07:23
73

How can I make it return today's date in that case?

pseudocode:

if (today == monday)
    return today;
else
    return strtotime(...);

Btw, this trick also could work:

strtotime('last monday', strtotime('tomorrow'));
zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 3
    @nute: well, I've checked all possible cases: any middle-week day, and edges: sunday & monday. All returns expected result. I think there are no problems, except of semantics ;-) – zerkms Nov 11 '10 at 01:03
  • 1
    Also see the [answer by dotjon](http://stackoverflow.com/a/8981753/216084) to this question only for a more elegant solution. –  Sep 09 '13 at 06:43
  • @Gopal Aggarwal: I remember there are some edge cases when `this` in `strtotime` points to now what you want. Cannot provide any particular example though – zerkms Sep 09 '13 at 06:54
  • @zerkms Sorry, but I am not getting your point. Kindly elaborate. –  Sep 09 '13 at 12:03
  • @Gopal Aggarwal: from what I remember, there are cases when `this` may refer to a former week, not to this – zerkms Sep 09 '13 at 12:05
  • @zerkms Okay. Well, I am putting my faith in core PHP's abilities :) And if there are certain edge-cases, I am hopeful they will be found and resolved in coming versions. –  Sep 09 '13 at 12:08
  • @GopalAggarwal: your faith is misplaced :) "this week" does not work for Sundays. Example, today is Sunday 2015-03-01, but "Sunday this week" will return 2015-03-08. See: http://stackoverflow.com/questions/13798026/php-week-starts-on-monday-but-monday-this-week-on-a-sunday-gets-monday-next and https://bugs.php.net/bug.php?id=63740&thanks=6 – Sarthaz Mar 01 '15 at 20:34
  • 1
    @Sarthaz Thanks for the heads-up and the links. I hope you do not mean what you say. If PHP as a language and a community cannot (or cannot hope to in a reasonable time) better its core functions once a bug has been reproduced, then maybe we should listen to 'PHP sucks' and move elsewhere. –  Mar 02 '15 at 04:24
  • If you're calling this repeatedly, try `strtotime('last monday', time() + 86400);`. Does exactly the same thing but saves a call to the expensive strtotime function. – Jamie Apr 06 '17 at 19:14
5

If today is Monday, strtotime("last Monday") will return a date 7 days in the past. Why don't you just check if today is Monday and if yes, return today's date and if not, return last week?

That would be a foolproof way of doing this.

if (date('N', time()) == 1) return date('Y-m-d');
else return date('Y-m-d', strtotime('last Monday'));

http://us2.php.net/manual/en/function.date.php

Vic
  • 1,336
  • 11
  • 30
  • 1
    just curious, what if it was `Now, 8th 23:59:59.999` in your `if()` clause and when interpreter reach `return` clause - it is already `Now, 9th` (tuesday)? ;-) – zerkms Nov 11 '10 at 00:50
  • Ha, I was trying to keep it short. Personally I would declare today and last Monday first and then make the comparisons to avoid the race condition. I'm a bigger fan of your working back from tomorrow method. – Vic Nov 11 '10 at 00:53
  • yeah, race condition is the most weird bug to detect. Sorry if my comment looks rude. – zerkms Nov 11 '10 at 00:55
  • Not at all. Don't worry about it. – Vic Nov 11 '10 at 01:02
3

As it was correctly outlined in the previous answer, this trick works, but also had caveats prior to PHP 5.6.22, PHP 7.0.7 and PHP 7.1-dev:

strtotime('last monday', strtotime('tomorrow'));

// or this one, which is shorter, but was buggy:
strtotime('Monday this week');

To those, who prefer the "Jedy-way", to work with objects of the DateTime class, the solution is next:

(new \DateTime())->modify('tomorrow')->modify('previous monday')->format('Y-m-d');

or even shorter notation:

\DateTime('Monday this week')

Be carefull, because if you do the same on SQL, you don't need to have any of these tricks in with addition of "tomorrow". Here's how the solution will look:

SELECT DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY) as last_monday;
Community
  • 1
  • 1
Farside
  • 9,923
  • 4
  • 47
  • 60
1

Late answer, but I thought I would post up this answer (which is actually from a different but related question). It handles the scenario in the question:

function last_monday($date) {
    if (!is_numeric($date))
        $date = strtotime($date);
    if (date('w', $date) == 1)
        return $date;
    else
        return strtotime(
            'last monday',
             $date
        );
}

echo date('m/d/y', last_monday('8/14/2012')); // 8/13/2012 (tuesday gives us the previous monday)
echo date('m/d/y', last_monday('8/13/2012')); // 8/13/2012 (monday throws back that day)
echo date('m/d/y', last_monday('8/12/2012')); // 8/06/2012 (sunday goes to previous week)

try it: http://codepad.org/rDAI4Scr

... or a variation that has sunday return the following day (monday) rather than the previous week, simply add a line:

 elseif (date('w', $date) == 0)
    return strtotime(
        'next monday',
         $date
    );

try it: http://codepad.org/S2NhrU2Z

You can pass it a timestamp or a string, you'll get back a timestamp

Documentation

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
0

Depending on exactly what you're using it for, this may be useful. Since one second's ambiguity is OK for my requirements, I use:

date( 'Y-m-d 23:59:59', strtotime( 'last sunday' ))

to get midnight on the most recent Monday (or today if today IS Monday).

Johno
  • 1,959
  • 1
  • 15
  • 15
0

My aproach:

date_default_timezone_set('Europe/Berlin');
function givedate($weekday, $time) {
$now = time();
$last = strtotime("$weekday this week $time");
$next = strtotime("next $weekday $time");
    if($now > $last) {
        $date = date("d.m.Y - H:i",$next);
    }
    else {
        $date = date("d.m.Y - H:i",$last);
    }
    return $date;
}

echo givedate('Wednesday', '00:52');

Or monthly

    function givedate_monthly($weekday, $time) {
    $now = time();
    $last = strtotime("first $weekday of this month $time");
    $next = strtotime("first $weekday of next month $time");
        if($now > $last) {
            $date = date("d.m.Y - H:i",$next);
        }
        else {
            $date = date("d.m.Y - H:i",$last);
        }
        return $date;
}

echo givedate_monthly('Wednesday', '01:50');
carbonero
  • 1
  • 1
-1
$monday = strtotime('Monday last week');
$sunday = strtotime('+6 days', $monday);
Stefan Gabos
  • 1,345
  • 13
  • 15