基本所有的数据统计都离不开基础条件-时间数据
封装了一个function方便调用(可以根据自己想要的格式进行更改,支持指定时间输出)
private function get_time_data($time_frame = 1, $start_time = '', $end_time = '')
{
$time = time();
if ($time_frame == 1) {
$week = date('w', $time);
$day = [];
for ($i = 1; $i <= 7; $i++){
$day[$i-1]['date_name'] = date('m.d' ,strtotime( '+'.$i-$week.' days', $time));
$day[$i-1]['date'] = date('Y-m-d' ,strtotime( '+'.$i-$week.' days', $time));
$day[$i-1]['start_time'] = strtotime($day[$i-1]['date'].' 00:00:00');
$day[$i-1]['end_time'] = strtotime($day[$i-1]['date'].' 23:59:59');
if (date('Y-m-d' ,strtotime( '+'.$i-$week.' days', $time)) == date('Y-m-d',$time)) {
$day[$i-1]['is_today_or_month'] = 1;
}else{
$day[$i-1]['is_today_or_month'] = 2;
}
}
$time_data = $day;
}elseif ($time_frame == 2) {
$month = date('d', $time);
$week = [];
for ($i = 1; $i <= date('t', $time); $i++){
$week[$i-1]['date_name'] = date('d', strtotime( '+'. $i-$month .' days', $time));
$week[$i-1]['date'] = date('Y-m-d', strtotime( '+'. $i-$month .' days', $time));
$week[$i-1]['start_time'] = strtotime($week[$i-1]['date'].' 00:00:00');
$week[$i-1]['end_time'] = strtotime($week[$i-1]['date'].' 23:59:59');
if (date('Y-m-d', strtotime( '+'. $i-$month .' days', $time)) == date('Y-m-d',$time)) {
$week[$i-1]['is_today_or_month'] = 1;
}else{
$week[$i-1]['is_today_or_month'] = 2;
}
}
$time_data = $week;
}elseif ($time_frame == 3) {
$year = date('Y', $time);
$month = [];
for ($i = 1; $i <= 12 ; $i++) {
$month[$i-1]['date_name'] = $i;
$month[$i-1]['date'] = $year.'-'.$i;
$month[$i-1]['start_time'] = strtotime(date( 'Y-m-d 00:00:00', strtotime($month[$i-1]['date']) ));
$month[$i-1]['end_time'] = strtotime(date( 'Y-m-' . date( 't',strtotime($month[$i-1]['date'])) . ' 23:59:59', strtotime($month[$i-1]['date']) ));
if (($year.'-'.$i) == date('Y-n',$time)) {
$month[$i-1]['is_today_or_month'] = 1;
}else{
$month[$i-1]['is_today_or_month'] = 2;
}
}
$time_data = $month;
}elseif ($time_frame == 4) {
$time_data = [];
$i = 0;
while ($start_time <= $end_time){
$time_data[$i]['date_name'] = date('Y/n/j',$start_time);
$time_data[$i]['start_time'] = strtotime(date('Y-m-d 00:00:00',$start_time));
$time_data[$i]['end_time'] = strtotime(date('Y-m-d 23:59:59',$start_time));
if (date('Y-m-d',$start_time) == date('Y-m-d',$time)) {
$time_data[$i]['is_today_or_month'] = 1;
}else{
$time_data[$i]['is_today_or_month'] = 2;
}
$start_time = strtotime('+1 day',$start_time);
$i++;
}
}
return $time_data;
}
进行调用
$time_data = $this->get_time_data($time_frame);
var_dump($time_data);