0

here is scenario that i have two time as given i have only time part of datetime in 24 hour format

$start_time = "23:00:00";
$end_time = "07:00:00";

How i can calculate duration hours , if the start_time's hour part is greater then $end_time's hour.

Mudassar Zahid
  • 305
  • 2
  • 14

1 Answers1

1

I would just create an if, where if the start time is bigger you have to calculate 24h (base_time) minus the start time first.

$start_time = "23:00:00";
$end_time = "07:00:00";
$base_time = "24:00:00";

if ($start_time>$end_time) {
 $time = $base_time-$start_time+$end_time;
} else {
 $time = $start_time-$end_time;
}

Or you can also have a look here, if you also have the day format given.

The way over DateTime, as far as I know, is just possible with a date. So you could add a pseudo date infront... and then use diff :

$start_time = "23:00:00";
$end_time = "07:00:00";
$st = new DateTime("0000-00-00".$start_time);
$et = new DateTime("0000-00-01".$end_time);

$interval = $st->diff($et);
$time = $interval->format('%H:%I:%S');
Community
  • 1
  • 1
Dead Master
  • 110
  • 14