Are there any potential pitfalls to be aware of when calculating deadlines in PHP, especially when considering weekends and holidays?
When calculating deadlines in PHP, it's important to consider weekends and holidays to ensure accurate results. One potential pitfall to be aware of is not accounting for weekends and holidays, which could lead to incorrect deadlines being calculated. To solve this issue, you can create a function that takes into account weekends and holidays when determining the deadline.
function calculateDeadline($startDate, $daysToAdd, $holidays) {
$endDate = strtotime($startDate);
while ($daysToAdd > 0) {
$endDate = strtotime('+1 day', $endDate);
// Skip weekends
if (date('N', $endDate) >= 6) {
continue;
}
// Skip holidays
if (in_array(date('Y-m-d', $endDate), $holidays)) {
continue;
}
$daysToAdd--;
}
return date('Y-m-d', $endDate);
}
// Example usage
$startDate = '2022-01-01';
$daysToAdd = 5;
$holidays = ['2022-01-03', '2022-01-10']; // List of holidays
$deadline = calculateDeadline($startDate, $daysToAdd, $holidays);
echo $deadline;