PHP实用封装函数集一
获取本周前的12周开始时间与结束时间在统计中,会经常遇到“统计本月前的12个月的数据”,这个非常好计算,一年12个月,好计算。如果说,要“统计本周前的12周的数据”,这还真是第一次碰到这种需求。
好吧,看招:
//本周前的前12周以及对应开始日期、结束日期
function get_12_week()
{
$now = time();
$curr_week_1 = date('Y-m-d', strtotime('-' . (date("w",$now)-1).' days', $now));//得到本周星期一
$i = 0;
while(true)
{
$i += 1;
$before_week_1 = date('Y-m-d', strtotime('-7 days', strtotime($curr_week_1)));
$before_week_7 = date('Y-m-d', strtotime('+6 days', strtotime($before_week_1)));
$week[$i] = [
'format_time_start' => $before_week_1,
'format_time_end' => $before_week_7,
'time_start' => strtotime($before_week_1),
'time_end' => strtotime($before_week_7.' 23:59:59')];
$curr_week_1 = $before_week_1;
if($i >= 12)
{
break;
}
}
$week = array_reverse($week);
return $week;
}
获取本周一,周未的时间: $today = date('Y-m-d');
$curr = strtotime($today);
$i = date('N');
$diff = $i - 1;
$monday = strtotime('-'.$diff.' days');
$begin = date('Y-m-d', $monday);
$end = date('Y-m-d 23:59:59', strtotime('+6 days', $monday));
echo $begin, "\n", $end; //输出:
2021-01-11
2021-01-17 23:59:59
好吧,看招:
//本周前的前12周以及对应开始日期、结束日期
function get_12_week()
{
$now = time();
$curr_week_1 = date('Y-m-d', strtotime('-' . (date("w",$now)-1).' days', $now));//得到本周星期一
$i = 0;
while(true)
{
$i += 1;
$before_week_1 = date('Y-m-d', strtotime('-7 days', strtotime($curr_week_1)));
$before_week_7 = date('Y-m-d', strtotime('+6 days', strtotime($before_week_1)));
$week[$i] = [
'format_time_start' => $before_week_1,
'format_time_end' => $before_week_7,
'time_start' => strtotime($before_week_1),
'time_end' => strtotime($before_week_7.' 23:59:59')];
$curr_week_1 = $before_week_1;
if($i >= 12)
{
break;
}
}
$week = array_reverse($week);
return $week;
}
获取本周一,周未的时间: $today = date('Y-m-d');
$curr = strtotime($today);
$i = date('N');
$diff = $i - 1;
$monday = strtotime('-'.$diff.' days');
$begin = date('Y-m-d', $monday);
$end = date('Y-m-d 23:59:59', strtotime('+6 days', $monday));
echo $begin, "\n", $end; //输出:
2021-01-11
2021-01-17 23:59:59