How can PHP be used to calculate working hours that span multiple days?
To calculate working hours that span multiple days in PHP, you can use the DateTime class to handle date and time calculations. Start by creating DateTime objects for the start and end dates and times. Then, calculate the total working hours by subtracting the start time from the end time and accounting for any non-working hours (e.g., weekends or holidays).
$start = new DateTime('2022-01-01 08:00:00');
$end = new DateTime('2022-01-03 17:00:00');
$interval = $start->diff($end);
$totalHours = $interval->days * 24 + $interval->h;
echo "Total working hours: " . $totalHours;
Related Questions
- What are the best practices for designing PHP libraries for non-programmer users to ensure readability and ease of use?
- What is the purpose of using flush() in PHP and how does it affect the output to the browser?
- What are some recommended resources or tutorials for beginners looking to work with images in PHP?