get the difference in time between two given time

2019-08-12 03:41发布

问题:

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".

回答1:

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


回答2:

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.



回答3:

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


回答4:

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


回答5:

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