Take the 2-minute tour ×

Possible Duplicate:
Get first day of week in PHP?

When a date is given I should get the date of Monday of that week.

When 2012-08-08 is given it should return 2012-08-06.

share|improve this question
Try date('w') to get what day-of-the-week a given date is - monday is day 1. After that it's just math. –  Marc B Aug 2 '12 at 4:32
Agreed on the duplicate, but I like my way better :p –  Chris Aug 2 '12 at 4:46
add comment

marked as duplicate by Dagon, Chris, Ben Rowe, j0k, Neil Aug 2 '12 at 23:50


2 Answers

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

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',

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

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


share|improve this answer
add comment

You can make a timestamp easily with the strtotime function - it accepts both a phrase like "last monday" as well as a secondary parameter which is a timestamp that you can make easily from the date you have using mktime (note that the inputs for a particular date are Hour,Minute,Second,Month,Day,Year).

    $monday=strtotime("monday this week", mktime(0,0,0, 8, 8, 2012));
    echo date("Y-m-d",$monday);
    // Output: 2012-08-06

Edit changed "last monday" in strtotime to "monday this week" and it now works perfectly.

share|improve this answer
This doesn't work if the input date is 2012-08-06. –  irrelephant Aug 2 '12 at 4:42
@irrelephant It does now :) +1 btw for the eagle eyes :) –  Fluffeh Aug 2 '12 at 4:50
Output is "2012-08-13" codepad.org/mCOrBDL3 –  Chris Aug 2 '12 at 4:56
@Chris interesting, working fine on my laptop here (PHP5.3.0) –  Fluffeh Aug 2 '12 at 5:00
@Fluffeh even weirder.... I tested it on my own server and the return is "2012-08-06". Hard to blame on timezone... I could see if the date in question were a sunday, but a wednesday shifting ahead a week? You're right... weird. –  Chris Aug 2 '12 at 5:03
show 4 more comments
