calculate the difference between 2 timestamps in php

前端 未结 5 938
时光说笑
时光说笑 2020-12-05 19:06

I am using 2 timestamp in my table which is starttime datatype- timestamp and as current timestamp. endtime datatype-timestamp and default as 0000-00-00 00:00:00

ho

5条回答
  •  甜味超标
    2020-12-05 19:28

    The simplest answer(for me of course) is here

    function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
    {
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);
    
        $interval = date_diff($datetime1, $datetime2);
    
        return $interval->format($differenceFormat);
    
    }
    

    In this case date_create() function creates the DateTime object

提交回复
热议问题