PHP convert date interval diff to decimal

后端 未结 3 771
一向
一向 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:53

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

        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 ;-) */
    ?>
    

提交回复
热议问题