How can PHP be used to handle time calculations in a Check In / Out system effectively?

When handling time calculations in a Check In / Out system, PHP can be used effectively by utilizing the DateTime class to manipulate dates and times. This allows for easy comparison of check-in and check-out times to calculate durations or determine if a user has overstayed their allotted time.

// Example code snippet for handling time calculations in a Check In / Out system

// Get current date and time
$currentDateTime = new DateTime();

// Example check-in time
$checkInTime = new DateTime('2022-01-01 10:00:00');

// Example check-out time
$checkOutTime = new DateTime('2022-01-01 14:30:00');

// Calculate duration between check-in and check-out times
$duration = $checkInTime->diff($checkOutTime);

// Output duration in hours and minutes
echo "Duration: " . $duration->h . " hours, " . $duration->i . " minutes";