Dear developer!
I found a script on the internet, by this script i can count leave days Like (2016-07-15) to (2016-07-22) without wekeend days. But i want count more records in one like (2016-07-15) to (2016-07-22) or (2015-01-11) to (2015-02-01).... I paste script on below please help me. Thank you.

<?php

$start = new DateTime('2016-07-15');
$end = new DateTime('2016-07-22');
// otherwise the  end date is excluded (bug?)
$end->modify('+1 day');

$interval = $end->diff($start);

// total days
$days = $interval->days;

// create an iterateable period of date (P1D equates to 1 day)
$period = new DatePeriod($start, new DateInterval('P1D'), $end);

// best stored as array, so you can add more than one
$holidays = array('2016-07-22','2016-07-21');

foreach($period as $dt) {
    $curr = $dt->format('D');

    // for the updated question
    if (in_array($dt->format('Y-m-d'), $holidays)) {
       $days--;
    }

    // substract if Saturday or Sunday
    if ($curr == 'Sat' || $curr == 'Sun') {
        $days--;
    }
}

echo $days; // 4

?>

Line 17:

$holidays = array('2016-07-22','2016-07-21');

In this particular case, these are two days, one after the other. But if you changed it, for example to:

$holidays = array('2016-07-15','2016-07-22');

This is an array of two days. Line 23 checks whether a date is either July 15, 2016 or July 22, 2016. It does not check whether a date was in between July 15, 2016 and July 22, 2016, inclusive, so July 17, 2016 would not be "in" the array. From your wording, it seems like you want these dates to act like an interval:

(2016-07-15) to (2016-07-22)

I interpret that English language phrase as being the eight days in the range of July 15 to July 22, inclusive. That's not what this code is doing. Also, say you have your holidays defined as

 $holidays = array('2016-07-16','2016-07-17');

That is a Saturday and Sunday. As your code goes through the foreach loop, it looks like it might subtract for each day being a holiday AND for each day being a Saturday or a Sunday. So a single day could decrement days by more than 1. That's not what you want.

What you DO want to do is unclear to me. Perhaps give an example (in English, not PHP) of the date range(s) and the holiday(s) and exactly how everything should be counted?

Thank you Mr. AssertNull, One employee in January take leave from date of 2016-01-01 To 2016-01-05 and i record this leave in mySQL and this emoployee take leave in next month from 2016-02-10 To 2016-02-20 and i want count days of this two records in this scripts.

OK, so it sounds like you you have one or more holiday RANGES that you want to check. As mentioned, the way you have it now, you are checking the exact dates, which is not what you want. Also, again, you are potentially subtracting TWICE, once if a day is a holiday and once if it is a weekend. The problem of subtracting twice is easily remedied. Simply change the "if" in line 28 to "else if".

For the first problem, I think you can write a short function called "between" that takes three dates as parameters. $holidaystart is the start of the holiday interval. $holidayend is the end of the interval. $datetocheck is the date to check. If $holdidaystart<= $datetocheck <= $holidayend, then return True. Otherwise return False. Replace line 23 with a call to this "between" function. Or don't use a function and simply put the logic in line 23. You can loop through all the holiday ranges. If any of them return True, then decrement days, but again, only do so once. That's the key.

As for how to compare dates to see if a date is less than or equal to another date, there are a few ways to do it, but I imagine the strtotime function might be easiest. See this link.

http://www.php.net/manual/en/function.strtotime.php

Dear AssertNull, Thanks for taking the time, But my problem is difference i tell you what i want from this code:

First i don't have problem wit weekends and holidays, I just want count all days of an employee that gets leave like i records "john" get leave from (2015-01-10) To (2015-01-15) its one time leave and another leave he gets in (2015-02-23) To (2015-02-29) and another time gets from (2015-04-02) To (2015-04-09) and how i count and get these total days? and i know how much days he was in leave.

As alternative, if the dates are saved into a database table, you could use datediff() along with date_add() (to get the inclusive counting) and sum(), for example:

select sum(datediff(date_add(`to`, interval 1 day), `from`)) as `total` from `holidays` where `employee_id` = 1;

+-------+
| total |
+-------+
|    21 |
+-------+

Dear Dereal thanks, Can you put this code to my script for complete.?

First i don't have problem wit weekends and holidays

?? You have code to subtract weekends in there. I assume there is some purpose. And I assume you don't want to subtract the same day twice.

Let's take the month of July 2016. Let's say the employee has two holiday periods: July 8, 2016 through July 14, 2016, and July 25, 2016 through July 31, 2016. So lines 3 and 4 would have $start as July 1, 2016 and $end = July 31, 2016.

There are 17 days in July 2016 that are not in those two holiday periods. 11 of those days are neither Saturday nor Sunday. So should the end value of $days should be equal to 11, which should be printed out? That was my interpretation of the problem. Is that correct? And the question is how to program that, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.