PHP convert date interval diff to decimal

后端 未结 3 770
一向
一向 2021-01-20 14:51

I\'m trying to convert the difference between two dates into a total year count, right now I\'m using this:

 $datetime1 = new DateTime(\'2009-10-11\'); 
 $da         


        
3条回答
  •  走了就别回头了
    2021-01-20 15:44

    If you don't care about perfect accuracy:

    return $interval->days / 365;
    

    You could also do something like return $interval->y + $interval->m / 12 + $interval->d / 365.

    Didn't even notice your weird decimal convention until I saw @2unco's comment. That would look like: return $interval->y . '.' . $interval->m.

提交回复
热议问题