function sign_count(){ $cur_day = date('Ymd',time()); $arr = [ ['id' => 7, 'sign_time' => '20200907'], ['id' => 6, 'sign_time' => '20200905'], ['id' => 4, 'sign_time' => '20200904'], ['id' => 3, 'sign_time' => '20200903'], ['id' => 2, 'sign_time' => '20200902'], ['id' => 1, 'sign_time' => '20200901'], ]; $add_count_days = 0;//连续签到天数 $count = 0; //累计签到天数 $day_list = []; if (!empty($arr)){ foreach ($arr as $k=>$v){ $day_list[] = $v['sign_time']; } $count = count($day_list); //累计签到天数 } $latelyDay = isset($day_list[0]) ? $day_list[0] : 0;//最近一次完成时间
//PS跨月的会有问题 (strtotime($endDate) - strtotime($startDate)) / (24 * 3600) 计算相差天数 if($cur_day - 1 > $latelyDay){ var_dump(array('add_days'=>$add_count_days,'total_day'=>$count)); } else { $add_count_days = 1;//连续签到天数 } for($i = 0;$i < $count - 1;$i++){ $res = $this->compareDay($day_list[$i], $day_list[$i+1]); if($res) $add_count_days++; else break; } var_dump(array('add_days'=>$add_count_days,'total_day'=>$count)); }
/**
* 比较当天签到天数与上一次签到时间
* @param string $curDay 当天签到天数
* @param string $nextDay 上一次签到天数
* @return boolean
*/
function compareDay($curDay,$nextDay) {
//跨月的会有问题 直接 (strtotime($endDate) - strtotime($startDate)) / (24 * 3600) 计算相差天数 if($curDay - 1 == $nextDay){ return true; }else{ return false; } }