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;

preferences:
26.59 ms | 404 KiB | 5 Q