calculate the difference between 2 timestamps in php

前端 未结 5 936
时光说笑
时光说笑 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:12

    I have created one function which helps you to get Hours, Minutes and Days from the two Unix timestamps by just passing type as 3rd parameter.

    public function diffBtwTimesAsPerType($start, $end, $returnType=1) {
        $seconds_diff = $start - $end;
        if($returnType == 1){
            return $seconds_diff/60;//minutes
        }else if($returnType == 2){
            return $seconds_diff/3600;//hours
        }else{
            return $seconds_diff/3600/24; //days
        }
    }
    
    echo "
    Minutes = ".diffBtwTimesAsPerType(1593714600, 1593541800, 1);//minutes echo "
    Hours = ".diffBtwTimesAsPerType(1593714600, 1593541800, 2);//hours echo "
    Days = ".diffBtwTimesAsPerType(1593714600, 1593541800, 3);//days

提交回复
热议问题