Show PHP time as clock more than 24 hours, for example 70 hours

I have a code that shows the 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 that the difference is more than 24 hours, it breaks the time for more than 24 hours and shows 1 or 2 or any hours but below 24 hours.

How can I show the difference in real hours like 74 hours!

Thank,

+3


source to share


4 answers


Ideally, I would prefer the following approach, instead of reinventing the wheel or doing a lot of manual transformations:

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

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

      

From 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, the days will be FALSE.

Note that this assumes all days are 24 hours, which may not be the case in DST areas

I wrote the following function to help 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');

      

NB I used format('U')

instead getTimestamp()

because of a comment in manual .

Also, no 64-bit is required for dates after epoch and before negative epochs!

+2


source


You can use the code below:

<?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 the above process. If you need help, I will be happy to help.



Hope it works.

Source: How to Calculate Hours Between Two Dates in PHP

+1


source


I suggest this one solution if you like it about watches:

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

      

regarding the note below - it could be like this:

echo $interval->days*24 + $interval->h;

      

+1


source


The code below will display the difference in hours between any two days. In that case, 72. Hope it 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;

      

0


source







All Articles