DCJones DCJones - 1 year ago 104
PHP Question

PHP: return the difference in Hours & Minutes between midnight today 00:00, and now

I am trying to return the difference in hours and minutes between midnight today "00:00" and now time.

Ths is what I have so far:

$now = date('H:i:s', time());
$time1 = strtotime('00:00:00');
$time2 = strtotime($now);
$difference = round(abs($time2 - $time1) / 3600,2);
echo $difference;

The returned value does not look correct, say the time now is 10:37, the returned difference is displaying as 10:63. Now thats not correct. Can anyone see where I am going wrong.

Many thanks in advance for your time.


Answer Source

Try this and design your output time.

$start_time = "00:00:00";
$end_time = date("H:i:s", time());

$time1 = new DateTime($start_time);
$time2 = new DateTime($end_time);
$interval = $time1->diff($time2);

var_dump($interval); //for details about the interval times

echo $hour = $interval->format('%h hour'); // hour
echo $min = $interval->format('%i min');   // min
echo $sec = $interval->format('%s second');// sec
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download