PHP convert date interval diff to decimal

若如初见. 提交于 2019-12-04 04:06:13

问题


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'); 
 $datetime2 = new DateTime('2010-10-10');
 $interval = $datetime1->diff($datetime2);
 return $interval->format('%y');

This returns me an int (Like 0 for < than a year, 2 for two years, etc.)

I need the result to be decimal as following:

0.9 - 9 months

1.2 - 1 year and two months

3.5 - 3 years and five months

and so on..

Thanks!


回答1:


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.




回答2:


Here you can see a function that does exactly that and with many options: http://php.net/manual/es/function.date-diff.php#98615

    <?php 
/* 
* A mathematical decimal difference between two informed dates 
*
* Author: Sergio Abreu
* Website: http://sites.sitesbr.net
*
* Features: 
* Automatic conversion on dates informed as string.
* Possibility of absolute values (always +) or relative (-/+)
*/

function s_datediff( $str_interval, $dt_menor, $dt_maior, $relative=false){

       if( is_string( $dt_menor)) $dt_menor = date_create( $dt_menor);
       if( is_string( $dt_maior)) $dt_maior = date_create( $dt_maior);

       $diff = date_diff( $dt_menor, $dt_maior, ! $relative);

       switch( $str_interval){
           case "y": 
               $total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
           case "m":
               $total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
               break;
           case "d":
               $total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
               break;
           case "h": 
               $total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
               break;
           case "i": 
               $total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
               break;
           case "s": 
               $total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
               break;
          }
       if( $diff->invert)
               return -1 * $total;
       else    return $total;
   }

/* Enjoy and feedback me ;-) */
?>



回答3:


Simpler and more accurate interval converter to days/hours/minutes/seconds:

function DateDiffInterval($sDate1, $sDate2, $sUnit='H') {
//subtract $sDate2-$sDate1 and return the difference in $sUnit (Days,Hours,Minutes,Seconds)
    $nInterval = strtotime($sDate2) - strtotime($sDate1);
    if ($sUnit=='D') { // days
        $nInterval = $nInterval/60/60/24;
    } else if ($sUnit=='H') { // hours
        $nInterval = $nInterval/60/60;
    } else if ($sUnit=='M') { // minutes
        $nInterval = $nInterval/60;
    } else if ($sUnit=='S') { // seconds
    }
    return $nInterval;
} //DateDiffInterval


来源:https://stackoverflow.com/questions/10778338/php-convert-date-interval-diff-to-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!