2

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.

Community
  • 1
  • 1
Anu
  • 905
  • 4
  • 23
  • 54
  • What result do you get with the mention code? – sandeepsure Nov 05 '15 at 11:12
  • I got as 'Saturday'. how can i identify that as first saturday of that month? – Anu Nov 05 '15 at 11:16
  • Can you write down sample input and sample output? – sandeepsure Nov 05 '15 at 11:17
  • So you're looking for even vs odd Saturdays *within the month*, rather than *within the year*, so that you had two Saturdays off in a row, October 31st and November 7th? November 7th is the first week of the month, but the 45th week of the year. Both are odd, but that wasn't the case for Saturdays in October, where the first Saturday was in week 40. – ghoti Nov 05 '15 at 12:49

2 Answers2

0

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");
}
?>
cfreear
  • 1,855
  • 2
  • 19
  • 25
  • It seems like 'even' or 'odd' is relative to the weeks in the month, not in the year. _I.e._ the first Saturday of the month is always odd. – CompuChip Nov 05 '15 at 13:09
  • @CompuChip `date('j', $timestamp)` will always return a number between 1 and 7 inclusively for the first Saturday in a month which will identify it as an Odd Saturday. I think this is what OP wants... – cfreear Nov 05 '15 at 13:23
0

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;
}

Live example.

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
bishop
  • 37,830
  • 11
  • 104
  • 139
  • it is excluded the end date. If the end date will be the saturday then i have to check that also.For example if i want to check from 1st november and 14th november then working days will be 11 (in my case) but it returns 10. Because it excluded the end date, in foreach loop itself it is not coming. – Anu Nov 06 '15 at 05:22
  • @Anu Yes, that is the normal behavior of DatePeriod. [Just add one day to your end date](http://php.net/manual/en/class.dateperiod.php#109846) or, if you prefer put it in the method. I did the latter here. – bishop Nov 06 '15 at 13:54