Juliver Galleto
Juliver Galleto

Reputation: 9047

get the difference in time between two given time

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

Upvotes: 2

Views: 8497

Answers (6)

Alaa Moneam
Alaa Moneam

Reputation: 527

You can do something like this as a generic function

private function calculateDiffInMinutes($from, $to)
{
    $from = Carbon::createFromFormat('Y-m-d H:s:i', $from);
    $to = Carbon::createFromFormat('Y-m-d H:s:i', $to);
    return $to->diffInMinutes($from);
}

and you can call the function anywhere and pass the parameters you need here the difference between now and created at time

$this->calculateDiffInMinutes($item->created_at, now())

Hope this helped

Upvotes: 0

Kaushik shrimali
Kaushik shrimali

Reputation: 1228

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

Upvotes: 3

Harikrishnan
Harikrishnan

Reputation: 9979

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

Upvotes: 1

Eihwaz
Eihwaz

Reputation: 1234

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

Upvotes: 1

Murad Hasan
Murad Hasan

Reputation: 9583

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.

Upvotes: 2

xdazz
xdazz

Reputation: 160943

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

Upvotes: 3

Related Questions