Recently I needed to add a certain amount of weekdays onto a date using php 5.3.
I wrote this simple function which has two parameters, the amount of weekdays I want to add and the date I want to add them to as an instance of DateTime as follows:
/**
* @param int $weekDays
* @param DateTime $date
* @return DateTime
*/
public function addWeekdaysToDateTime($weekDays, DateTime $date)
{
$days = 0;
//only increase the day count for weekdays
while ($days < $weekDays) {
$date->modify("+1 day");
//if we're now on a weekday, increase the weekday counter
if (!in_array($date->format("D"), ['Sat', 'Sun'])) {
$days++;
}
}
return $date;
}
