0

Is there any native function for getting the difference between two timestamps? I want to get minutes difference of two timestamps.

Edit: Sorry but actually I just want the difference of two Unix timestamp.

$date = new DateTime();
$date1_timestamp = $date->getTimestamp();
sleep('120');
$date2_timestamp = $date->getTimestamp();

function get_unix_timestamp_minutes_difference($start, $end) {
    /*
        Some code for return the difference between two unix timestamps
    */
}

echo get_timestamp_minutes_difference($date1_timestamp, $date2_timestamp);
NiroshanJ
  • 558
  • 1
  • 5
  • 20
  • 1
    Possible duplicate of [calculate the difference between 2 timestamps in php](http://stackoverflow.com/questions/40905174/calculate-the-difference-between-2-timestamps-in-php) – Enstage May 16 '17 at 06:12
  • 1
    $to_time = strtotime("2008-12-13 10:42:00"); $from_time = strtotime("2008-12-13 10:21:00"); echo round(($to_time - $from_time) / 60,2). " minute"; – JYoThI May 16 '17 at 06:12
  • 2
    Refer http://stackoverflow.com/questions/7929612/time-diff-in-minutes-between-2-dates#7929652 – Indra Kumar S May 16 '17 at 06:13
  • 3
    Possible duplicate of [Time diff in minutes between 2 dates](http://stackoverflow.com/questions/7929612/time-diff-in-minutes-between-2-dates) – sumit May 16 '17 at 06:14
  • 2
    Possible duplicate of [Difference between 2 dates in seconds](http://stackoverflow.com/questions/5988450/difference-between-2-dates-in-seconds) – Lepidopteron May 16 '17 at 06:51

2 Answers2

1

You can use Carbon class http://carbon.nesbot.com/docs/ and use his difference API :http://carbon.nesbot.com/docs/#api-difference

0

Try This:

$datetime1 = strtotime("2017-05-16 10:00:00");
$datetime2 = strtotime("2017-05-16 10:45:00");
$interval  = abs($datetime2 - $datetime1);
$minutes   = round($interval / 60);
echo 'Diff. in minutes is: '.$minutes;