Reputation: 925
I want to calculate total number of working days in between two date. Here we include second and fourth Saturday as working day(i.e all Even Saturdays are considered as Holiday) I can get the day of the particular date by using below code
$day = ‘2015-11-07’;
$dayName = date("l",strtotime($day));
if ($dayName =='saturday') {
...
}
then I have to find whether that particular date falls under first Saturday of November or second Saturday. Is there any option in doing that?
I have the code to calculate total number of days excluding all Saturdays,Sunday and holidays. I got that code from another question
But I want to identify even saturdays and I will detect that saturdays in total days.
Upvotes: 2
Views: 2605
Reputation: 39474
Get all days between the two end-points, then apply your logic to each, accumulating the ones that match:
function work_days_between_two_dates(\DateTime $begin, \DateTime $end) {
$workdays = [];
$all_days = new DatePeriod($begin, new DateInterval('P1D'), $end->modify('+1 day'));
foreach ($all_days as $day) {
$dow = (int)$day->format('w');
$dom = (int)$day->format('j');
if (1 <= $dow && $dow <= 5) { // Mon - Fri
$workdays[] = $day;
} else if (6 == $dow && 0 == $dom % 2) { // Even Saturday
$workdays[] = $day;
}
}
return $workdays;
}
For November 2015 (dates between 11/1 and 12/1), this algorithm marks the following as work days:
November 2015
Su Mo Tu We Th Fr Sa
- 2 3 4 5 6 -
- 9 10 11 12 13 14
- 16 17 18 19 20 -
- 23 24 25 26 27 28
- 30
Upvotes: 0
Reputation: 1875
Here you go:
<?php
$day = '2015-11-14';
$timestamp = strtotime($day);
$dayOfWeek = date('w', $timestamp); //0-6, 6 = Saturday
$dayOfMonth = date('j', $timestamp); //1-31
$weekNum = ceil($dayOfMonth / 7); //round up to get the week number
if ($dayOfWeek == 6) { //first check the day of the week is a Saturday
if ($weekNum % 2 == 0) { //check that the week number is even
print("Even Saturday");
} else {
print("Odd Saturday");
}
} else {
print("Other day");
}
?>
Upvotes: 0