最近在写健身馆app的接口,需要统计最长连续运动天数,自己计算的不是很准确。
public function getSustainedDays($accountId)
{
$list = $this->getMotionDates($accountId);
$times = 0;
if ($list) {
$i = 0;
$j = 0;
$dates = strtotime(date("Y-m-d",$list[0]['entry_time']));
foreach ($list as $k => $v){
if (strtotime(date("Y-m-d",$v['entry_time'])) == ($dates - 24*60*60*$j)) {
$i++;
$j++;
} else {
if ($i > $times) $times = $i;
$i = 1;
$j = 0;
if ($k+1 < count($list)) $dates = strtotime(date("Y-m-d",$list[$k+1]['entry_time']));
}
}
if ($i > $times) $times = $i;
}
return $times;
}
public function getMotionDates($accountId)
{
$entryRecord = EntryRecord::find()
->alias('er')
->joinWith(['members m'], FALSE)
->where(['m.member_account_id' => $accountId])
->select('er.entry_time')
->groupBy(["DATE_FORMAT(from_unixtime(er.entry_time),'%Y-%m-%d')"])
->orderBy('er.entry_time desc')
->asArray()
->all();
return $entryRecord;
}
自己写了半天,感觉挺好用的,但是测试那边给打回来了,统计的不准确。发现有一个账号中间有间隔的一天,
竟然统计成2天了。求大神指点。
阿波罗的战车
大话西游666
慕容森