Difference between two timestamps in php

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

how to calculate the difference between 2 timestamps in php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06

asked Dec 1, 2016 at 7:46

3

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

answered Dec 1, 2016 at 8:02

Difference between two timestamps in php

Abhay MauryaAbhay Maurya

11k6 gold badges44 silver badges60 bronze badges

2

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);

answered Dec 1, 2016 at 8:01

bio_spritebio_sprite

4282 silver badges14 bronze badges

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

answered Mar 23, 2018 at 17:34

AlexKhAlexKh

5362 gold badges8 silver badges18 bronze badges

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

answered Jul 6, 2020 at 14:22

Difference between two timestamps in php

0

@atul-baldaniya, I've modified your solution to avoid negative values and return integer results instead of decimal values.

function diffBtwTimesAsPerType($start, $end, $returnType=1) {
    $seconds_diff = $end - $start;
    if($returnType == 1){
        return round($seconds_diff/60);//minutes
    }else if($returnType == 2){
        return round($seconds_diff/3600);//hours
    }else{
        return round($seconds_diff/3600/24); //days
    }
}

answered May 21, 2021 at 22:52

Difference between two timestamps in php

If you need the difference in 00:00:00 (Hours, Minutes, Seconds) format, you can use this function.

I used sprintf() because of adding zero before one digit results.

function timeDifference($start, $stop){
    
$diff = strtotime($stop) - strtotime($start);
$fullHours   = floor($diff/(60*60));
$fullMinutes = floor(($diff-($fullHours*60*60))/60);
$fullSeconds = floor($diff-($fullHours*60*60)-($fullMinutes*60));

return sprintf("%02d",$fullHours) . ":" . sprintf("%02d",$fullMinutes) . ":" . sprintf("%02d",$fullSeconds);
}

answered Oct 8, 2021 at 11:03

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');

answered Apr 18, 2018 at 13:53

How can I get the difference between two timestamps in php?

How to calculate the difference between two dates in PHP ?.
PHP | date_diff() Function..
Program to find the number of days between two dates in PHP..
Return all dates between two dates in an array in PHP..
PHP | fopen( ) (Function open file or URL).
PHP | fread( ) Function..
PHP | fclose( ) Function..
PHP | fwrite( ) Function..

How do you find the difference between two timestamps?

The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours.

How can I find the difference between two timestamps in SQL?

{fn TIMESTAMPDIFF(interval,startDate,endDate)} returns the difference between the starting and ending timestamps (startDate minus endDate) for the specified date part interval (seconds, days, weeks, and so on). The function returns an INTEGER value representing the number of intervals between the two timestamps.

How can I calculate hours between two dates in php?

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1); Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.