PHP calculating number of days between 2 dates

前端 未结 4 535
野的像风
野的像风 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条回答
  • 2021-01-03 03:01

    You could create a loop which goes to the next day in the $count_only array, from the $start_date and stopping (returning from the function) upon reaching the $end_date.

    function number_of_days_between($start_date, $finish_date, $count_only) {
        $count  = 0;
        $start  = new DateTime("@$start_date");
        $end    = new DateTime("@$finish_date");
        $days   = new InfiniteIterator(new ArrayIterator($count_only));
        foreach ($days as $day) {
            $count++;
            $start->modify("next $day");
            if ($start > $end) {
                return $count;
            }
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-03 03:11

    Of course there is a way :-)

    The days that have been elapsed is simply

    $elapsed_days = floor(($finish_date-$start_date) / 86400);
    

    This will not get the result you need. What you could do is the following (pesudo)code:

    $elapsed_days = floor(($finish_date-$start_date) / 86400);
    for(int $i=0;$i<$elapsed_days;$i++){
      $act_day_name = strtolower(date('l',$start_date+$i*86400));
      if(in_array($act_day_name,$count_only){
        // found matching day
      }
    }
    

    What I do: I iterate over every day which is between the both dates, get the day-name with date('l'); and check if it's within the array. There may be some fine tuning need to be done, but this should get you going.

    0 讨论(0)
  • 2021-01-03 03:24

    You can simplify this considerably by calculating how many complete weeks fall between the two specified dates, then do some math for the beginning/end partial weeks to account for dangling dates.

    e.g.

    $start_date = 1298572294;  // Tuesday
    $finish_date = 1314210695; // Wednesday
    
    $diff = 1314210695-1298572294 = 15638401 -> ~181 days -> 25.8 weeks -> 25 full weeks.
    

    Then it's just a simple matter of checking for the dangling dates:

    Tuesday -> add 2 days for Wednesday+Friday to get to the end of the week
    Wednesday -> add 1 day for Monday to get to the beginning on the week
    
    Total countable days = (25 * 3) + 2 + 1 = 75 + 3 = 78 countable days
    
    0 讨论(0)
提交回复
热议问题