Harish Kurup
Harish Kurup

Reputation: 7505

Error in calculating difference between two Unix timestamp in php

I have a function which calculate the difference between two timestamps,

public static function getDuration($from, $to) {
    $duration = $to - $from; 
    return $duration;
}

but this is not working as required. example, if i pass,

$difference = getDuration(strtotime('19-07-2012 23:00:00'), strtotime('20-07-2012 4:45:00'));
echo $difference/3600;

it returns 5.75 instead of 5.45, when converted to hours.

Upvotes: 0

Views: 1101

Answers (1)

Jerzy Zawadzki
Jerzy Zawadzki

Reputation: 1985

it returns ok - 5,75 is 5 3/4 hours = 5 hours 45 minutes

It returns in decimal - not hours/minutes

if you want hours/minutes you can use:

$hours=floor($difference/3600);
$minutes=$difference/60-$hours*60;

Upvotes: 4

Related Questions