Calculating number of years between 2 dates in PHP

前端 未结 5 1560
你的背包
你的背包 2020-12-30 19:32

I need to get the number of years from 2 dates provided. Here\'s my code:

function daysDifference($endDate, $beginDate)
{
   $date_parts1=explode(\"-\", $beg         


        
5条回答
  •  温柔的废话
    2020-12-30 20:06

    the $start_date and $end_date' value is the number of days, not seconds. so you should not divide the $diff with 365.25*60*60*24.

    function daysDifference($endDate, $beginDate)
    {
    
       $date_parts1 = explode("-", $beginDate);
       $date_parts2 = explode("-", $endDate);
       $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
       $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
       $diff = abs($end_date - $start_date);
       $years = floor($diff / 365.25);
       return $years;
    }
    
    echo daysDifference('2011-03-12','2008-03-09');
    

提交回复
热议问题