如何找到给定日期所属的一周中的第一天


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

可能重复:
用PHP学习一周的第一天?

给定一个时间戳,我需要找到时间戳所属的一周的第一天。

例如

$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);

本周的开始应该等于2011-06-27,但现在出现了严重的错误,等于1970-01-01,这对我来说意味着"一周的第一天"格式是无效的。

任何想法都将不胜感激,

$time = strtotime("2011-07-01 00:00:00");
$weekStartDate = date('Y-m-d',strtotime("last Monday", $time));

测试它:

$time=[Your Request Time];
$dayofweek = date("w",strtotime($time));
if( $dayofweek != 0  )
    $firstdayOfWeek = strtotime( $time . " - " . $dayofweek . " days " );
else
    $firstdayOfWeek = strtotime($time);