PHP Online Compiler
Example: Difference Between Two Time Periods in PHP using DateTime class
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?php // DIFFERENCE BETWEEN TWO TIME PERIODS in PHP using DateTime class // Step-1 Define two datetime strings $start = new DateTime("2025-11-05 10:15:00"); $end = new DateTime("2025-11-13 17:40:25"); // Step-2 Calculate the difference $interval = $start->diff($end); // Step-3 Extract hours, minutes, and seconds $totalHours = ($interval->days * 24) + $interval->h; $minutes = $interval->i; $seconds = $interval->s; // Step-4 Display the result echo "\nTIME DIFFERENCE: {$totalHours} HOURS, {$minutes} MINUTES, {$seconds} SECONDS"; ?>
Output
Clear
ADVERTISEMENTS