首页 > Web开发 > 详细

php计算时间段的最大连续天数

时间:2020-09-21 12:55:39      阅读:147      评论:0      收藏:0      [点我收藏+]
如需求:最高连续打卡多少天? 或者一串数字,最大连续数字有多少个。

$nums = [‘2018-10-09‘,‘2018-10-10‘,‘2018-10-11‘,‘2018-10-13‘,‘2018-10-15‘,‘2018-10-16‘,‘2018-10-17‘,‘2018-10-18‘];
    $max  = 0;
    $hash = [];
    foreach ($nums as $val) {
        $hash[$val] = 1;
    }
    foreach ($nums as $val){
        $num = 1;
        if (!$hash[$val - 1]) {
            while($hash[++$val]) $num++;
        }
        $max = $num > $max ? $num : $max;
    } 

    echo $max;
$nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1];
    $max  = 0;
    $hash = [];
    foreach ($nums as $val) {
        $hash[$val] = 1;
    }
    foreach ($nums as $val){
        $num = 1;
        if (!$hash[$val - 1]) {
            while($hash[++$val]) $num++;
        }
        $max = $num > $max ? $num : $max;
    } 

    echo $max;

php计算时间段的最大连续天数

原文:https://blog.51cto.com/xuqin/2536431

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!