PHP calculating number of days between 2 dates

前端 未结 4 555
野的像风
野的像风 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题