I wanted to convert this into hours:
<?php
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60 (This is for minutes)
?>
I wanted to convert this into hours:
<?php
( strtotime('12:45:00') - strtotime('11:00:00') ) / 60 (This is for minutes)
?>
You can convert minutes to hour using following function:
<?php
function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
echo convertToHoursMins(250, '%02d hours %02d minutes');
For more info check this post
Although I'm not sure whether I understand you right, here's an example:
<?php
$resultM = ( strtotime('12:45:00') - strtotime('11:00:00') ) / 60; // (This is for minutes)
$resultH = ( strtotime('12:45:00') - strtotime('11:00:00') ) / 3600; // (This is for hours)
$resultH1 = floor(( strtotime('12:45:00') - strtotime('11:00:00') ) / 3600); // (This is for hours)
echo "Minutes=$resultM<br />Hours(float)=$resultH<br />Houres(floor)=$resultH1<br />";
?>
Try this example. Positive and Negative answer will depends on date provided first in date_diff
function.
$date1 = date_create("12:45:00");
$date2 = date_create("11:00:00");
$diff=date_diff($date1,$date2);
echo "Difference in Hours\n";
echo $diff->format("%R%H hours")."\n";
echo "Difference in Minutes\n";
echo $diff->format("%R%i minutes");
click here for Working Demo
Another Example
$date2 = new DateTime("12:45:00");
$date1 = new DateTime("11:00:00");
echo "\n\n".$date2->diff($date1)->format("%H hours %i minutes");