PHP Current Usage Days / Days Missing [duplicate]

4

I would like to know if you have a specific function or how to do it on the current working day, for example, today is the 26th day ie the 18th working day of the month and so it goes and as the month changes it starts again at zero and goes counting according to the current business day (never adding Sunday or Saturday). Example (from 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 days that are missing to finish the month for example from today it is 5 days to finish the month.

    
asked by anonymous 26.07.2016 / 13:41

2 answers

5

I've assembled a small class that answers 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>';

Return:

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 reading the PHP Date and Time documentation .

I hope I have helped!

    
26.07.2016 / 15:37
0

The date function allows you to return numbers according to the day of the week, then you just have to set up a for / while loop structure that identifies the days.

Here is an example to know the day of the week that 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");
?>
    
26.07.2016 / 14:14