eval.in

Paste #90006

PHP — PHP 5.5.1, pasted 5 years ago (json)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
function number_of_working_days($from, $days) {
	$workingDays = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, ...)
	$holidayDays = ['*-12-25', '*-01-01', '2013-12-24', '2013-12-25']; # variable and fixed holidays

	$from = new DateTime($from);
	$dates = [];
	$dates[] = $from->format('Y-m-d');
	while ($days) {
		$from->modify('+1 day');

		if (!in_array($from->format('N'), $workingDays)) continue;
		if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
		if (in_array($from->format('*-m-d'), $holidayDays)) continue;

		$dates[] = $from->format('Y-m-d');
		$days--;
	}
	return $dates;
}


print_r( number_of_working_days('2013-12-23', 3) );

Program Output

Array
(
    [0] => 2013-12-23
    [1] => 2013-12-26
    [2] => 2013-12-27
    [3] => 2013-12-30
)

OK (0.008 sec real, 0.011 sec wall, 14 MB, 81 syscalls)

Fork