calculate the difference between 2 timestamps in php

前端 未结 5 931
时光说笑
时光说笑 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 "<br> Minutes = ".diffBtwTimesAsPerType(1593714600, 1593541800, 1);//minutes
    echo "<br> Hours = ".diffBtwTimesAsPerType(1593714600, 1593541800, 2);//hours
    echo "<br> Days = ".diffBtwTimesAsPerType(1593714600, 1593541800, 3);//days
    
    0 讨论(0)
  • 2020-12-05 19:19

    You can convert your timestamps to unix timestamp (time in seconds) using php strtotime and then take the difference. You now have the difference in time in seconds and can convert to what you need...hours, min, days

    http://php.net/manual/en/function.strtotime.php

    ex:

    $ts1 = strtotime($start);
    $ts2 = strtotime($end);     
    $seconds_diff = $ts2 - $ts1;                            
    $time = ($seconds_diff/3600);
    
    0 讨论(0)
  • 2020-12-05 19:19

    try this code, tested on phpfiddle.org :-

    function timestampdiff($qw,$saw)
    {
        $datetime1 = new DateTime("@$qw");
        $datetime2 = new DateTime("@$saw");
        $interval = $datetime1->diff($datetime2);
        return $interval->format('%Hh %Im');
    }
    echo timestampdiff('1524794340', '1524803100');
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-05 19:35

    Any procedural way should be avoided. Use OOP method for date time difference:

    $datetime1 = new DateTime('2016-11-30 03:55:06');//start time
    $datetime2 = new DateTime('2016-11-30 11:55:06');//end time
    $interval = $datetime1->diff($datetime2);
    echo $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds
    

    You can setup difference format as per your needs.

    %Y - use for difference in year
    %m - use for difference in months
    %d - use for difference in days
    %H - use for difference in hours
    %i - use for difference in minutes
    %s - use for difference in seconds
    

    You can remove any of above values as per your need. For example if you only are interested in hour difference and you know that difference cant be more than 24 hours then use only %H.

    If you want to have total difference in seconds then you can use:

    echo $difference_in_seconds = strtotime('2016-11-30 11:55:06') - strtotime('2016-11-30 03:55:06');//28800
    

    Depends upon your need and the final format in which you want to have time difference.

    For reference check: http://php.net/manual/en/datetime.diff.php

    I hope it helps

    0 讨论(0)
提交回复
热议问题