Business Day Script

3

I'm using a script that I found in this question of the SOen, and to count the working days disregarding Saturday and Sunday (can only do with Sunday tbm, changing $what_day > 5 to $what_day > 6 for example) it works right, but I wanted to include the calendar official holiday of Brazil.

This is the script that is working (already taking the data from my form):

    <?php
//get current month for example
$beginday = ($_POST ["Tdesl"]);
$lastday  = ($_POST ["Tinsem3"]);

$nr_work_days = getWorkingDays($beginday, $lastday);
echo $nr_work_days;

function getWorkingDays($startDate, $endDate)
{
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {
        echo "startdate is in the future! <br />";

        return 0;
    } else {
        $no_days  = 0;
        $weekends = 0;
        while ($begin <= $end) {
            $no_days++; // no of days in the given interval
            $what_day = date("N", $begin);
            if ($what_day > 5) { // 6 and 7 are weekend days
                $weekends++;
            };
            $begin += 86400; // +1 day
        };
        $working_days = $no_days - $weekends;

        return $working_days;
    }
}

HTML:

<form method="post" id="Cform" name="Tform" action="diasuteis2.php">
    <label for="Cinsem">Data inicial:</label>
    <input type="date" name="Tinsem3" id="Cinsem" size="6">
    <label for="Cdesl22">Data final:</label>
    <input type="date" name="Tdesl" id="Cdesl22" size="6"><br><br>
    <p align="center">
    <input type="submit" id="enviar"></p>
</form>

An example of correct output for:

echo $beginday;
echo "<br><br>";
echo $lastday;
echo "<br><br>";
echo $nr_work_days;

It is:

  

2014-12-10

     

2014-12-15

     

4

(He considered day 10, and deleted days 13 and 14 correctly)

Is it possible, or feasible, to include the holidays in this code? If someone can make an example (of course you do not need to include all the holidays, just show me how I can do this) thank you in advance.

    
asked by anonymous 05.05.2015 / 07:41

2 answers

5

Here's an example for counting holidays. Please note that this has not been tested, so there may be some errors.

function getWorkingDays($startDate, $endDate) {
    $begin = strtotime($startDate);
    $end   = strtotime($endDate);
    if ($begin > $end) {
        echo "startdate is in the future! <br />";
        return 0;
    }
    else {
        $holidays = array('01/01', '25/12', ...);
        $weekends = 0;
        $no_days = 0;
        $holidayCount = 0;
        while ($begin <= $end) {
            $no_days++; // no of days in the given interval
            if (in_array(date("d/m", $begin), $holidays)) {
                $holidayCount++;
            }
            $what_day = date("N", $begin);
            if ($what_day > 5) { // 6 and 7 are weekend days
                $weekends++;
            };
            $begin += 86400; // +1 day
        };
        $working_days = $no_days - $weekends - $holidayCount;

        return $working_days;
    }
}
    
05.05.2015 / 12:58
1

I'd like to add on the holidays:

    function dias_feriados($ano = null)
    {
        if ($ano === null)
        {
            $ano = intval(date('Y'));
        }

        $pascoa     = easter_date($ano); // Limite de 1970 ou após 2037 da easter_date PHP consulta http://www.php.net/manual/pt_BR/function.easter-date.php
        $dia_pascoa = date('j', $pascoa);
        $mes_pascoa = date('n', $pascoa);
        $ano_pascoa = date('Y', $pascoa);

        $feriados = array(
            // Tatas Fixas dos feriados Nacionail Basileiras
            mktime(0, 0, 0, 1,  1,   $ano), // Confraternização Universal - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 4,  21,  $ano), // Tiradentes - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 5,  1,   $ano), // Dia do Trabalhador - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 9,  7,   $ano), // Dia da Independência - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 10,  12, $ano), // N. S. Aparecida - Lei nº 6802, de 30/06/80
            mktime(0, 0, 0, 11,  2,  $ano), // Todos os santos - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 11, 15,  $ano), // Proclamação da republica - Lei nº 662, de 06/04/49
            mktime(0, 0, 0, 12, 25,  $ano), // Natal - Lei nº 662, de 06/04/49

            // These days have a date depending on easter
            mktime(0, 0, 0, $mes_pascoa, $dia_pascoa - 48,  $ano_pascoa),//2ºferia Carnaval
            mktime(0, 0, 0, $mes_pascoa, $dia_pascoa - 47,  $ano_pascoa),//3ºferia Carnaval 
            mktime(0, 0, 0, $mes_pascoa, $dia_pascoa - 2 ,  $ano_pascoa),//6ºfeira Santa  
            mktime(0, 0, 0, $mes_pascoa, $dia_pascoa     ,  $ano_pascoa),//Pascoa
            mktime(0, 0, 0, $mes_pascoa, $dia_pascoa + 60,  $ano_pascoa),//Corpus Cirist
        );

        sort($feriados);

        return $feriados;
    }

You indicate the year and it generates an ARRAY with the days of the holidays. Apart from this there are still municipal holidays that should be added to ARRAY. Particularly I put in a database the municipal holidays, because in the team there are people working in several localities.

To show dates:

            $feriados = dias_feriados();
            $diaf = count($feriados);
            for ($x=0;$x<$diaf;$x++){
                echo '<p>'.date('Y-m-d',$feriados[$x]).'</p>';
            }   
    
11.06.2018 / 23:37