When a date is given how to get the date of Monday of that week in php

10,441

Solution 1

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

Solution 2

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).

<?php
    $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:
10,441

Related videos on Youtube

Yasitha
Author by

Yasitha

A Person who likes to contribute towards open source software.

Updated on September 15, 2022

Comments

  • Yasitha
    Yasitha over 1 year

    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.

    • Marc B
      Marc B over 11 years
      Try date('w') to get what day-of-the-week a given date is - monday is day 1. After that it's just math.
  • irrelephant
    irrelephant over 11 years
    This doesn't work if the input date is 2012-08-06.
  • Fluffeh
    Fluffeh over 11 years
    @irrelephant It does now :) +1 btw for the eagle eyes :)
  • Chris Baker
    Chris Baker over 11 years
    Output is "2012-08-13" codepad.org/mCOrBDL3
  • Fluffeh
    Fluffeh over 11 years
    @Chris interesting, working fine on my laptop here (PHP5.3.0)
  • Chris Baker
    Chris Baker over 11 years
    @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 Baker
    Chris Baker over 11 years
    Aye, you will :) Though I am still pretty curious why that failed. Is mktime inherently unreliable across hosts?
  • Fluffeh
    Fluffeh over 11 years
    @Chris Not sure, I will test it on linux when I get back home. Never had that much use for it myself to be honest.
  • Chris Baker
    Chris Baker over 11 years
    For reference, my test was on FreeBSD, PHP 5.4.5, Eastern Standard Time. Codepad.org is PHP 5.2.5 on a Linux build, UTC.