How to find the first day of the week a given date belongs to?

woot586 picture woot586 · Jul 1, 2011 · Viewed 11.2k times · Source

Possible Duplicate:
Get first day of week in PHP?

Given a timestamp I need to find the first day the week the timestamp belongs to.

e.g.

$format = "first day of week";

//convert to time
$time = strtotime("2011-07-01 00:00:00");

//format the time to the first day of the week
$date = strtotime($format,$time);

//Put the first day of the week into a pretty format
$weekStartDate = date("Y-m-d",$date);

The week start should equal 2011-06-27 at the moment its gone horribly wrong and equals 1970-01-01 which suggests to me the “first day of week” format is invalid.

Any ideas would be much appreciated thanks,

Answer

Ion Roata picture Ion Roata · Jul 1, 2011
$time = strtotime("2011-07-01 00:00:00");

$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));