Calculation of the longest continuous exercise days

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)) {
                $iPP;
                $jPP;
            } 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
Mar.22,2021

solve it in a different way without extra query overhead. Add two fields to the user table: the number of days in a row and the date of the last sign-in. When signing in, judge whether the last day is today, if nothing is done; if it is yesterday, PP, update the last date; if it is the day before yesterday or longer, change the number of days to 1, update the last date


function getSustainedDays()
{
    $list = [
        ['entry_time'=>'2018-01-01'],    
        ['entry_time'=>'2018-01-02'],    
        ['entry_time'=>'2018-01-04'],    
        ['entry_time'=>'2018-01-05'],    
        ['entry_time'=>'2018-01-06'],    
        ['entry_time'=>'2018-01-07'],    
        ['entry_time'=>'2018-01-09']
    ];
    $times = 0;
    $max = count($list)-1;
    if ($list) {
        $arr = [];
        $i = 0;
        $j = 0;
        $dates = strtotime($list[0]['entry_time']);
        foreach ($list as $k => $v){
            
            $now =  strtotime( $v['entry_time']);
            if ($now == ($dates +  24*60*60*$j)) {
                $iPP;
                $jPP;
                echo $k;
            } else {
                array_push($arr,$i);
                $i = 1;
                $j=1;
                if ($k+1 < count($list)) $dates = $now;
        
            }
            if($k==$max){
                array_push($arr,$i);
            }
                             
        }
        print_r($arr);
            $times = max($arr);
        
    }
    return $times;
}
echo getSustainedDays();

:
public function getSustainedDays($accountId)
{
    $list = $this->getMotionDates($accountId);
    $len = 1;
    $max = 1;
    if ($list) {
        foreach ($list as $k => $v){
            if ($k+1 == count($list)) break;
            if ((strtotime(date("Y-m-d",$list[$k]['entry_time'])) - 60*60*24) == strtotime(date("Y-m-d",$list[$k+1]['entry_time']))) {
                $lenPP;
            } else {
                if ($len > $max) $max = $len;
                $len = 1;
            }
            if ($len > $max) $max = $len;
        }
    }
    return $max;
}
Menu