PHP calculating number of days between 2 dates

前端 未结 4 557
野的像风
野的像风 2021-01-03 02:31

I am developing a web application which revolves around dates.

I need to calculate numbers based around days elasped, for example - pseudo code

$coun         


        
4条回答
  •  Happy的楠姐
    2021-01-03 03:06

    Just a bit faster approach than "iterating through all days":

    $count_only = array(1, 3, 5); // days numbers from getdate() function
    $start_date = 1298572294;
    $finish_date = 1314210695;
    
    function days($start_date, $finish_date, $count_only)
    {
        $cnt = 0;
    
        // iterate over 7 days
        for ($deltaDays = 0; $deltaDays < 7; $deltaDays++)
        {
            $rangeStart = $start_date + $deltaDays * 86400;
    
            // check the weekday of rangeStart
            $d = getDate($rangeStart);
            if (in_array($d['wday'], $count_only))
            {
                $cnt += ceil(($finish_date - $rangeStart) / 604800);
            }
        }
    
        return $cnt;
    }
    

    The idea is to count number of weeks using some additional offsets for mondays, tuesdays, wednesdays etc.

提交回复
热议问题