3

i have been having trouble displaying the hours and minutes after calculating the difference between 2 different unix timestamps. Lets say i have these 2 unix timestamps:

1) 1384327800
2) 1384300800

the difference is

27000

if i divide 27000/3600 i get

7.5

but what i want is to display

7:30

or

7 hours and 30 minutes

what is the best way to go about this?

user2835653
  • 192
  • 1
  • 3
  • 15

4 Answers4

5

All you need to do it a little calculation:

$diff = 27000;

$hour = floor($diff / 3600);
$min  = floor(($diff - $hour * 3600) / 60);
$sec = $diff - $hour * 3600 - $min * 60;

echo "$hour hours, $min minutes, $sec seconds";

Or try it with DateTime class:

$dt1 = new DateTime('@1384327800');
$dt2 = new DateTime('@1384300801');
$diff = $dt1->diff($dt2);
echo $diff->format('%h hours, %i minutes, %s seconds');
Glavić
  • 42,781
  • 13
  • 77
  • 107
3

Algorithmic way (with no automatic calculation with any function/library) :

$diff_in_minutes = ($timestamp1 - $timestamp2) / 60;
$minutes = $diff_in_minutes % 60;
$hours = ($diff_in_minutes - $minutes) / 60;

$diff_string = $hours . ':' . $minutes;
Gavin
  • 2,123
  • 1
  • 15
  • 19
OlivierH
  • 3,875
  • 1
  • 19
  • 32
2

Look at DateTime() and DateInterval::format()

$dt1 = new DateTime('@1384327800');
$dt2 = new DateTime('@1384300800');
$diff = $dt1->diff($dt2);
echo $diff->format('%h hours and %i minutes');

This last little bit will remove unnecessary time periods from your string if so desired.

$elapsed = $diff->format('%y years, %m months, %a days, %h hours, %i minutes, %S seconds');
$elapsed = str_replace(array('0 years,', ' 0 months,', ' 0 days,',  ' 0 hours,', ' 0 minutes,'), '', $elapsed);
$elapsed = str_replace(array('1 years, ', ' 1 months, ', ' 1 days, ',  ' 1 hours, ', ' 1 minutes'), array('1 year, ', '1 month, ', ' 1 day, ', ' 1 hour, ', ' 1 minute'), $elapsed);
echo $elapsed;
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 1
    This code is invalid. `%a` is used, where `%d` should be. If difference is `10 years`, you replace that with empty string (`0 years` => `''`). If difference is `11 years`, it is replaced with `11 year` (`1 years` => `1 year`). – Glavić Nov 13 '13 at 22:15
1

For full long formatting exactly as described:

date_default_timezone_set('UTC');
$timestamp1 = new DateTime('@1384327800');
$timestamp2 = new DateTime('@1384300800');
$diff = $timestamp1->diff($timestamp2);
$timemap = array('y' => 'year',
                 'm' => 'month',
                 'd' => 'day',
                 'h' => 'hour',
                 'i' => 'minute',
                 's' => 'second');
$timefmt = array();

foreach ($timemap as $prop => $desc) {
    if ($diff->$prop > 0) {
        $timefmt[] = ($diff->$prop > 1) ? "{$diff->$prop} {$desc}s" : "{$diff->$prop} $desc";
    }
}

$diffstr = (count($timefmt) > 1)
    ? $diff->format(sprintf('%s and %s',
          implode(', ', array_slice($timefmt, 0, -1)), end($timefmt)))
    : end($timefmt);
var_dump($diffstr);

This gave me the following:

string(22) "7 hours and 30 minutes"
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96