3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start = new \DateTime('2006-04-12T12:30:00'); $end = new \DateTime('2006-04-14T11:30:00'); //determine what interval should be used - can change to weeks, months, etc $interval = new \DateInterval('PT1H'); //create periods every hour between the two dates $periods = new \DatePeriod($start, $interval, $end); //count the number of objects within the periods $hours = iterator_count($periods); echo $hours . ' hours' . PHP_EOL; //difference between Unix Epoch $diff = $end->getTimestamp() - $start->getTimestamp(); $hours = $diff / ( 60 * 60 ); echo $hours . ' hours (60 * 60)' . PHP_EOL; //difference between days $diff = $end->diff($start); $hours = $diff->h + ($diff->days * 24); echo $hours . ' hours (days * 24)' . PHP_EOL;
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
47 hours 47 hours (60 * 60) 47 hours (days * 24)

preferences:
157.61 ms | 403 KiB | 221 Q