How can PHP be used to separate and calculate time differences for different time periods within a day?

To separate and calculate time differences for different time periods within a day in PHP, you can use the DateTime class to create DateTime objects for the start and end times of each period. Then, you can use the diff() method to calculate the time difference between the two DateTime objects.

// Define start and end times for two different time periods within a day
$start1 = new DateTime('08:00:00');
$end1 = new DateTime('12:00:00');

$start2 = new DateTime('13:00:00');
$end2 = new DateTime('17:00:00');

// Calculate time difference for the first time period
$diff1 = $start1->diff($end1);
echo "Time difference for period 1: " . $diff1->format('%H hours %i minutes') . "\n";

// Calculate time difference for the second time period
$diff2 = $start2->diff($end2);
echo "Time difference for period 2: " . $diff2->format('%H hours %i minutes') . "\n";