Back to blog

PHP find previous monday from a date

This small function returns the date (with the format you want) of the previous monday from a given date. If…

This small function returns the date (with the format you want) of the previous monday from a given date. If given date is empty it gets the today date:

// usage:
// last monday: $d = findMonday();
// last monday before 31-12-2010: $d = findMonday("2010-12-31");
// last monday before 31-12-2010 with a specific format: $d = findMonday("2010-12-31","m/d/Y");

function findMonday($d="",$format="Y-m-d") {
	if($d=="") $d=date("Y-m-d");
	$delta = date("w",strtotime($d)) - 1;
	if ($delta <0) $delta = 6;
	return date($format, mktime(0,0,0,date('m'), date('d')-$delta, date('Y') ));
}

Canonical URL