I have two dates stored in the variables $date1
and $date2
respectively. They are in the format:
YYYY / MM / DD HH: MM
Would you like to know how I get the difference in hours between these two dates?
I have two dates stored in the variables $date1
and $date2
respectively. They are in the format:
YYYY / MM / DD HH: MM
Would you like to know how I get the difference in hours between these two dates?
You can use the DateTime::diff
to know the interval between two dates, to know the difference in hours, you get the difference in days and multiply by 24 .
$datatime1 = new DateTime('2015/04/15 00:00:00');
$datatime2 = new DateTime('2015/05/16 00:00:00');
$data1 = $datatime1->format('Y-m-d H:i:s');
$data2 = $datatime2->format('Y-m-d H:i:s');
$diff = $datatime1->diff($datatime2);
$horas = $diff->h + ($diff->days * 24);
echo "A diferença de horas entre {$data1} e {$data2} é {$horas} horas";
<?php
$date1 = new DateTime("now");
$date2 = new DateTime("tomorrow");
$interval = date_diff($date1, $date2);