Scott Hurring » Code » PHP » Snippets » Find monday

Problem

You want to find the date of the most recent Monday.

Solution

Figure out what today is, then figure out how many days ago was Monday.
The previous monday is: $today-$offset


Code


<?php
/*
Find Monday - prints out the previous monday.
Scott Hurring - scott at hurring dot com
*/

$dow date("w");
print 
"Today is dow $dow (Monday is 1)\n";

// How many days ago was monday?
$offset = ($dow -1);
if (
$offset <0) {
    
$offset 6;
}
print 
"Offset = $offset\n";
print 
"Previous monday is: ";
print 
date("M d, Y"mktime(0,0,0,date('m'), date('d')-$offsetdate('Y') ));
?>