get the difference in time between two given time

2019-08-12 03:15发布

I'm trying to get the difference in time between two given times e.g. first time '17:00:01' and then second time '17:47:25' and here's what I tried so far,

$start_time = "17:00:01";
$end_time = "17:47:25";

echo $start_time->diff($end_time);

But seem's unfortunately not working, any help, ideas please?

My expected output must be like, if no difference in hours but there's difference in minutes and seconds then, "22 mins and 15 secs", If no difference in minutes but have difference in hours then, "2 hrs and 10 secs" but if only seconds in difference then, "22 secs".

5条回答
smile是对你的礼貌
2楼-- · 2019-08-12 04:04

Convert string time to timestamp and subtract. Convert timestamp to desired time format.

$start_time = strtotime('17:00:01');
$end_time = strtotime('17:47:25');
$diff = $end_time - $start_time;
echo date('H:i:s', $diff);
查看更多
▲ chillily
3楼-- · 2019-08-12 04:07

Just another way of doing it using string functions:

/*
 * Get difference in seconds between time formats
 */
function getDiff($start_time, $end_time)
{
    $start_time = timeFormatToSeconds($start_time);
    $end_time = timeFormatToSeconds($end_time);

    if ($end_time > $start_time) {
        return $end_time - $start_time;
    }

    return false;
}

/*
 * Convert time format (HH:MM:SS) to seconds
 */
function timeFormatToSeconds($time_format)
{
    sscanf($time_format, "%d:%d:%d", $hours, $minutes, $seconds);

    return $hours * 3600 + $minutes * 60 + $seconds;
}

$start_time = "17:00:01";
$end_time = "17:47:25";

$diff = getDiff($start_time, $end_time);

if ($diff) {
    $hours = floor($time / (60 * 60));
    $time -= $hours * (60 * 60);

    $minutes = floor($time / 60);
    $time -= $minutes * 60;

    $seconds = floor($time);
    $time -= $seconds;

    var_dump(array($hours, $minutes, $seconds));
}
查看更多
Summer. ? 凉城
4楼-- · 2019-08-12 04:13

String in php is not object, so can't call diff method on it.

Use diff method, you must change the string to DateTime object.

If you want to compare time difference in same day, you could try:

$start_time = "17:00:01";
$end_time = "17:47:25";

$start_datetime = new DateTime(date('Y-m-d').' '.$start_time);
$end_datetime = new DateTime(date('Y-m-d').' '.$end_time);

var_dump($start_datetime->diff($end_datetime));
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-08-12 04:20

Here is it.

PHP

$start_time = "17:00:01";
$end_time = "17:47:25";

$time1 = new DateTime($start_time);
$time2 = new DateTime($end_time);
$interval = $time1->diff($time2);

echo $hour = $interval->format('%h hour');
echo $min = $interval->format('%i min');
echo $sec = $interval->format('%s second');

Output:

0 hour 47 min 24 sec

Now you can add some condition and make the real format.

查看更多
戒情不戒烟
6楼-- · 2019-08-12 04:20

Get difference between two times

 public function convTimeToMinutes($intime, $outtime)
    {
            $start = Carbon::parse($intime);
            $end = Carbon::parse($outtime);
            $minutes = $end->diffInMinutes($start); // 226
            return $this->convertMinuteToHours($minutes);  
    } 

public function convertMinuteToHours($minutes)
    {
        return $minutes / 60;
    }

$totalHour = $this->convTimeToMinutes('09:25:35', '13:12:27');
//$totalHour = 3.76
查看更多
登录 后发表回答