I’m trying to determine the duration of a time interval in PHP, taking into account business hours (from 9 am to 5 pm) on Monday to Friday and excluding specific public holidays. Let’s consider the public holidays as 15th August 2023, 1st November 2023, 11th November 2023, and 25th December 2023.
For example, suppose I have an interval from 28th October 07:00:00 to 3rd November 10:00:00. How can I calculate the duration (in seconds) only during business hours, excluding weekends and the mentioned holidays?
I’m looking for guidance on an effective approach to achieve this duration calculation.
2
Answers
By using the spatie/opening-hours and briannesbitt/Carbon library, I successfully implemented the desired functionality. The script, utilizing a sample JSON file (sample.json) as input, calculates time differences while excluding public holidays and weekends. The focus is on determining the duration only during working hours.
sample.json
Below is the complete code:
Y-m-d
format or similar.Pseudocode: