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)
尝试这个:http://codepad.org/rDAI4Scr
...或者是一个变种,使星期日返回下一天(星期一),而不是上一周,只需添加一行代码:
elseif (date('w', $date) == 0)
return strtotime(
'next monday',
$date
);
试一下:http://codepad.org/S2NhrU2Z
你可以传递一个时间戳或一个字符串,它会返回一个时间戳。
文档
date('w')
来获取给定日期是星期几 - 星期一为第1天。之后就只是数学计算了。 - Marc B