/*
* 获取某星期的开始时间和结束时间 * time 时间 * first 表示每周星期一为开始日期 0表示每周日为开始日期
*/
function getWeek($time = '', $first = 1)
{
//当前日期
if (!$time) $time = time();
$date = date("Y-m-d", $time);
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
//获取当前周的第几天 周日是 0 周一到周六是1 - 6
$w = date('w', strtotime($date));
//获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$week_start = date('Y-m-d', strtotime("$date -" . ($w ? $w - $first : 6) . ' days'));
//本周结束日期
$week_end = date('Y-m-d', strtotime("$week_start +6 days"));
return array("week_start" => $week_start, "week_end" => $week_end);
}
/*
* 获取某一时间上周开始时间与结束时间 * time 时间
*/
function getLastWeek($time = '')
{
//当前日期
if (!$time) $time = time();
if (date('l',$time) == 'Monday') {
$week_start = date('Y-m-d',strtotime('last monday',$time));
}else{
$week_start = date('Y-m-d',strtotime('-1 week last monday',$time));
}
$week_end = date('Y-m-d',strtotime('last sunday',$time));
return array("week_start" => $week_start, "week_end" => $week_end);
}
网友评论