0
votes

I am trying to find the time difference between 2 timestamps.

Timestamp "startTime" have 1601096400

Timestamp "expireTime" have 1601094600

I want to output the difference between both time like Expire in : 4hr 30min

How do I do it in php ?

2
Have you done any research? Show us your best attempt (code) and please read How to Askjibsteroos

2 Answers

0
votes

You can use this function

function timestamp_diff($a,$b){
    $datetime1 = new DateTime($a);
    $datetime2 = new DateTime($b);
    $interval = $datetime1->diff($datetime2);
    $d=$interval->format('%a');
    $h=$interval->format('%h');
    $m=$interval->format('%i');
    return 'Expire in :'.$d.'day and '.$h.'hr'.$m.'min'
}
0
votes

Try This is work proper.

<?php 
    $diff = 1601096400 - 1601094600;
    //if use date so try this
    $diff = strtotime("2020-09-26 08:10:00") - strtotime("2020-10-26 04:42:00");
    $days    = floor($diff / 86400);
    $hours   = floor(($diff - ($days * 86400)) / 3600);
    $minutes = floor(($diff - ($days * 86400) - ($hours * 3600))/60);
    $seconds = floor(($diff - ($days * 86400) - ($hours * 3600) - ($minutes*60)));
    echo 'Expire in : '$hours.'hr'.$minutes.'min';
?>