php: datetime() difference between 2 datetime with 2 variables

前端 未结 5 1929

I have in my database 4 columns which are:

Date_in | Time_in | Date_out | Time_out

Date_in and Time_in belong together (e.g., 2013-02-18 13

5条回答
  •  佛祖请我去吃肉
    2020-12-20 05:09

    Try this,

    function datediff( $date1, $date2 )
    {
        $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );
    
        return sprintf
        (
            "%d Days, %d Hours, %d Mins, %d Seconds",
            intval( $diff / 86400 ),
            intval( ( $diff % 86400 ) / 3600),
            intval( ( $diff / 60 ) % 60 ),
            intval( $diff % 60 )
        );
    }
    
    print datediff( "18th February 2013", "now" ) . "\n";
    

    OR

    You can use DateTime::diff

      $start_date = new DateTime("2012-02-10 11:26:00");
        $end_date = new DateTime("2012-04-25 01:50:00");
        $interval = $start_date->diff($end_date);
        echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
    

    EDIT:

    Checkout links,

    How to calculate the difference between two dates using PHP?

    Php Date Time – 7 Methods to Calculate the Difference between 2 dates.

    may help you.

提交回复
热议问题