9

I have below code which shows time

$now = date_create(date("Y-m-d H:i:s"));

$replydue = date_create($listing['replydue_time']);

$timetoreply = date_diff($replydue, $now);

echo $timetoreply->format('%H:%I')

Byt my problem is if difference is more than 24 hrs, it breaks time in more 24 hrs and shows 1 or 2 or any hours but below 24 hrs.

How can i show real hours difference like 74 hrs!

Thanks,

rjcode
  • 1,193
  • 1
  • 25
  • 56

4 Answers4

3

Ideally I'd prefer the following approach.. rather than reinvent the wheel or do lots of manual conversions:

$now = new DateTime();
$replydue = new DateTime($listing['replydue_time']);

$timetoreply_hours = $timetoreply->days * 24 + $timetoreply->h;
echo $timetoreply_hours.':'.$timetoreply->format('%I');

From the manual:

days: If the DateInterval object was created by DateTime::diff(), then this is the total number of days between the start and end dates. Otherwise, days will be FALSE.

Please note this assumes that all days are 24hrs which may not be the case in areas with DST

I have written the following function to assist with this:

/**
 * @param DateTimeInterface $a
 * @param DateTimeInterface $b
 * @param bool              $absolute Should the interval be forced to be positive?
 * @param string            $cap The greatest time unit to allow
 * 
 * @return DateInterval The difference as a time only interval
 */
function time_diff(DateTimeInterface $a, DateTimeInterface $b, $absolute=false, $cap='H'){
  // Get unix timestamps
  $b_raw = intval($b->format("U"));
  $a_raw = intval($a->format("U"));

  // Initial Interval properties
  $h = 0;
  $m = 0;
  $invert = 0;

  // Is interval negative?
  if(!$absolute && $b_raw<$a_raw){
    $invert = 1;
  }

  // Working diff, reduced as larger time units are calculated
  $working = abs($b_raw-$a_raw);

  // If capped at hours, calc and remove hours, cap at minutes
  if($cap == 'H') {
    $h = intval($working/3600);
    $working -= $h * 3600;
    $cap = 'M';
  }

  // If capped at minutes, calc and remove minutes
  if($cap == 'M') {
    $m = intval($working/60);
    $working -= $m * 60;
  }

  // Seconds remain
  $s = $working;

  // Build interval and invert if necessary
  $interval = new DateInterval('PT'.$h.'H'.$m.'M'.$s.'S');
  $interval->invert=$invert;

  return $interval;
}

This can be used:

$timetoreply = time_diff($replydue, $now);
echo $timetoreply->format('%r%H:%I');

N.B. I have used format('U') instead of getTimestamp() because of the comment in the manual.

Also not that 64-bit is required for post-epoch and pre-negative-epoch dates!

Arth
  • 12,789
  • 5
  • 37
  • 69
  • are you sure this is correct? $timetoreply->days * 24 because if i go with this i can strange numbers of hours, and if i go with $interval->format('%a')*24, i get correct hours. – rjcode May 06 '15 at 10:53
  • hmm Perfect! Got it now and converted! – rjcode May 06 '15 at 11:05
  • Does this means convert both to UNIX time then subtract, as given in below post by William Francis? – rjcode May 06 '15 at 13:47
  • @rjcode It sure does.. but I like to use the PHP objects if possible. – Arth May 06 '15 at 13:58
1

You can use below code:

<?php
$date1 = "2014-05-27 01:00:00";
$date2 = "2014-05-28 02:00:00";
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
echo "Difference between two dates is " . $hour = abs($timestamp2 - $timestamp1)/(60*60) . " hour(s)";
?>

Try to follow above process. If you need any help i will be glad to assist.

Hope it will work.

Source: How to Calculate Hours Between Two Dates in PHP

1

I'll offer this one solution, in case you like it in hours:

echo $interval->format('%a')*24+$interval->format('%h');

regarding the note below - it can be in this way too:

echo $interval->days*24 + $interval->h;
Kancho Iliev
  • 701
  • 5
  • 13
  • I was going to upvote this.. but there are properties in the DateInterval class that allow you to access these numbers without using the format function. – Arth May 06 '15 at 10:46
  • It's truth, but shown on this way I think the idea is much clear – Kancho Iliev May 06 '15 at 10:48
  • How is it clearer, you are using a two character string to prompt conversion from an interval into a string. Then implicitly convert those into numbers, do a calculation and change it back to a string.. You honestly think that `format('%a')` is clearer than `days`? – Arth May 06 '15 at 10:52
  • 1
    Thanks :) But I have some logic behind, when I used 'a' but It probably will be hard for my bad English. I liked to use 'a' with idea that it is not directly associates by days. There are d and D too, but they are similar like a h - returns up to 31 days ( like a 'h' - up to 24 ). That's way I used 'a'. If I've used ->days, the direct association will not clear the question if it is limited to 31. That was my logic - may be a bit strange but ... Fo rest you @Arth are quite right. – Kancho Iliev May 06 '15 at 11:09
  • 1
    Good point.. I agree it is a bit confusing. I'd rather clear that up with a comment than using a round-about method with an obscure parameter however! – Arth May 06 '15 at 11:44
0

The below code will output the difference in hours between any two days. In this case, 72. Hope this helps!

<?php

$startTime = new \DateTime('now');
$endTime = new \DateTime('+3 day');
$differenceInHours = round((strtotime($startTime->format("Y-m-d H:i:s")) - strtotime($endTime->format("Y-m-d H:i:s")))/3600, 1);
echo $differenceInHours;