Chr*_*ker 10
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)
Run Code Online (Sandbox Code Playgroud)
试试吧:http://codepad.org/rDAI4Scr
......或者是在第二天(星期一)而不是前一周星期日返回的变体,只需添加一行:
elseif (date('w', $date) == 0)
return strtotime(
'next monday',
$date
);
Run Code Online (Sandbox Code Playgroud)
试试吧:http://codepad.org/S2NhrU2Z
你可以传递时间戳或字符串,你会得到一个时间戳
文档
strtotime- http://php.net/manual/en/function.strtotime.phpdate- http://php.net/manual/en/function.date.php您可以使用该函数轻松创建时间戳strtotime- 它接受诸如“上周一”之类的短语以及辅助参数,该辅助参数是您可以从您使用的日期轻松创建的时间戳mktime(请注意,特定日期的输入是Hour,Minute,Second,Month,Day,Year)。
<?php
$monday=strtotime("monday this week", mktime(0,0,0, 8, 8, 2012));
echo date("Y-m-d",$monday);
// Output: 2012-08-06
?>
Run Code Online (Sandbox Code Playgroud)
编辑将“上周一”更改strtotime为“本周周一”,现在效果很好。
| 归档时间: |
|
| 查看次数: |
9478 次 |
| 最近记录: |