PHP Current business days/ Missing days

Asked

Viewed 1,362 times

4

I would like to know about if you have a specific function or how to do about the current working days for example today is day 26 or is 18 current working day of the month and so on and as change of month starts again at zero and counts as the current working day (never adding Sunday or Saturday). Example(of this month):

Dia 1(Sexta) = 1 Dia útil.
Dia 2(Sábado) = 0 .
Dia 3(Domingo) = 0 .
Dia 4(Segunda) 2 Dia útil.

And also the remaining days to finish the month for example from today are 5 days to finish the month.

2 answers

6


I’ve built a small class that responds to an array with the information you need.

class DiasUteis {

    public static function contarDiasUteis($data, $formato = 'd/m/Y') {

        $dt = DateTime::createFromFormat($formato, $data);
        if ($dt === false) {
            echo "ERRO: A data informada '$data', precisa estar no formato '$formato'.";
            exit;
        }

        $dataUtil = DiasUteis::diaUtil($dt); // a data é útil(true/false)

        $ano = date_format($dt, 'Y');
        $mes = date_format($dt, 'n');
        $ultimo_dia = (integer) date_format($dt, 't');

        $diasUteisNoMes = 0;
        $diasUteisAteData = 0;

        for ($dia = 1; $dia <= $ultimo_dia; $dia++) {
            $dia_contar = DateTime::createFromFormat('j/n/Y', "$dia/$mes/$ano");
            if (DiasUteis::diaUtil($dia_contar)) {
                $diasUteisNoMes++;
                if ($dia_contar <= $dt) {
                    $diasUteisAteData++;
                }
            }
        }
        return [
            'data_util' => $dataUtil
            , 'data_dia_semana' => date_format($dt, 'l')
            , 'dias_uteis_no_mes' => $diasUteisNoMes
            , 'dias_uteis_ate_data' => $diasUteisAteData
            , 'dias_uteis_faltam' => $diasUteisNoMes - $diasUteisAteData
        ];
    }

    private static function diaUtil($dt) {
        if ((date_format($dt, 'N') === '6') || (date_format($dt, 'N') === '7')) {
            return false;
        } else {
            return true;
        }
    }

}

How to use:

 $resultado = DiasUteis::contarDiasUteis('2016-07-19', 'Y-m-d');
 echo '<pre>';
 var_dump($resultado);
 echo '</pre>';

Returns:

array(5) {
  ["data_util"]=>
  bool(true)
  ["data_dia_semana"]=>
  string(7) "Tuesday"
  ["dias_uteis_no_mes"]=>
  int(22)
  ["dias_uteis_ate_data"]=>
  int(13)
  ["dias_uteis_faltam"]=>
  int(9)
}

I recommend a reading on PHP Date and Time Documentation.

I hope I’ve helped!

  • Any questions just ask.

  • To return a specific value in this array how can I do ? For example I only want the value of dias_uteis_no_mes or only dias_uteis_ate_data

  • As well, what I would like to return?

  • No need more I already decided to pull is quite simple I just put " echo $result['dias_uteis_ate_data'];" kkk

  • Okay! I’m glad I could help!

  • It had an error, the correct one is: if ((date_format($dt, 'N') === '6') || (date_format($dt, 'N') === '7'))... I updated the answer with the correct code.

Show 1 more comment

0

The date function allows you to return numbers according to the day of the week, then just assemble a repeat structure (for/while) that loops and identifies the days.

Follow an example to know the day of the week I use.

<?
function diasemana($data) {
    $ano =  substr("$data", 0, 4);
    $mes =  substr("$data", 5, -3);
    $dia =  substr("$data", 8, 9);

    $diasemana = date("w", mktime(0,0,0,$mes,$dia,$ano) );

    switch($diasemana) {
        case"0": $diasemana = "Domingo";       break;
        case"1": $diasemana = "Segunda-Feira"; break;
        case"2": $diasemana = "Terça-Feira";   break;
        case"3": $diasemana = "Quarta-Feira";  break;
        case"4": $diasemana = "Quinta-Feira";  break;
        case"5": $diasemana = "Sexta-Feira";   break;
        case"6": $diasemana = "Sábado";        break;
    }

    echo "$diasemana";
}

//Exemplo de uso
diasemana("2007-07-13");
?>
  • But then I would not accumulate the days of the month ? For every week would start zeroing again not ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.