PHP calculating number of days between 2 dates

前端 未结 4 536
野的像风
野的像风 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: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
    

提交回复
热议问题