Pergunta

Desejo calcular o número de dias da semana em um determinado mês e ano.Dias da semana significam de segunda a sexta-feira.Como eu faço isso?

Foi útil?

Solução

Algum código básico:

$month = 12;
$weekdays = array();
$d = 1;

do {
    $mk = mktime(0, 0, 0, $month, $d, date("Y"));
    @$weekdays[date("w", $mk)]++;
    $d++;
} while (date("m", $mk) == $month);

print_r($weekdays);

Remova o @ se o seu aviso de erro do PHP não mostrar avisos.

Outras dicas

Você não precisa contar todos os dias do mês.Você já sabe que os primeiros 28 dias contêm 20 dias da semana, aconteça o que acontecer.Tudo que você precisa fazer é determinar os últimos dias.Altere o valor inicial para 29. Em seguida, adicione 20 dias da semana ao seu valor de retorno.

function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=29;$d<=$lastday;$d++) {
    $wd = date("w",mktime(0,0,0,$m,$d,$y));
    if($wd > 0 && $wd < 6) $weekdays++;
    }
return $weekdays+20;
}

tente este

function getWeekdays($m, $y = NULL){
    $arrDtext = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri');

    if(is_null($y) || (!is_null($y) && $y == ''))
        $y = date('Y');

    $d = 1;
    $timestamp = mktime(0,0,0,$m,$d,$y);
    $lastDate = date('t', $timestamp);
    $workingDays = 0;
    for($i=$d; $i<=$lastDate; $i++){
        if(in_array(date('D', mktime(0,0,0,$m,$i,$y)), $arrDtext)){
            $workingDays++;
        }
    }
    return $workingDays;
}

Este é o código mais simples que consegui criar. Você realmente precisaria criar uma matriz ou uma tabela de banco de dados para manter os feriados e obter uma contagem verdadeira de "Dias úteis", mas não foi isso que foi perguntado, então aqui está, espero que isso ajude alguém.

function get_weekdays($m,$y) {
$lastday = date("t",mktime(0,0,0,$m,1,$y));
$weekdays=0;
for($d=1;$d<=$lastday;$d++) {
    $wd = date("w",mktime(0,0,0,$m,$d,$y));
    if($wd > 0 && $wd < 6) $weekdays++;
    }
return $weekdays;
}

Obtenha o número de dias úteis sem feriados entre duas datas:

Exemplo de uso:

echo number_of_working_days('2013-12-23', '2013-12-29');

Resultado:

3

Link para a função

Método DateObject:

function getWorkingDays(DateTime $date) {
    $month = clone $date;
    $month->modify('last day of this month');
    $workingDays = 0;
    for ($i = $month->format('t'); $i > 28; --$i) {
        if ($month->format('N') < 6) {
            ++$workingDays;
        }
        $month->modify('-1 day');
    }

    return 20 + $workingDays;
}

Calcule os dias úteis em um mês a partir de qualquer data:

public function getworkd($mday)
{
    $dn = new DateTime($mday);
    $dfrom = $dn->format('Y-m-01');
    $dtill = $dn->format('Y-m-t');
    $df = new DateTime($dfrom);
    $dt = new DateTime($dtill);
    $wdays = 0;
    while($df<=$dt)
    {
        $dof= $df->format('D') ;
        if( $dof == 'Sun' || $dof == 'Sat' ) ; else $wdays++;
        $df->add(new DateInterval('P1D'));
    }
    return $wdays;
}

Encontre o último dia e o dia da semana do mês em questão
em seguida, faça um loop while simples como: -

$dates = explode(',', date('t,N', strtotime('2013-11-01')));
$day = $dates[1]; 
$tot = $dates[0]; 
$cnt = 0;
while ($tot>1)
{   
    if ($day < 6)
    {   
        $cnt++;
    }   
    if ($day == 1)
    {   
        $day = 7;
    }   
    else
    {   
        $day--;
    }   
    $tot--;
}

$ cnt= total do dia da semana (segunda a sexta) para um determinado mês

Eu vim com uma função não-loop.Muito melhor em termos de desempenho.Pode parecer confuso, mas basta perguntar ao PHP o primeiro dia da semana e o número de dias do mês: o resto são operações aritméticas baseadas na lógica.

function countWorkDays($year, $month)
{
    $workingWeekdays   = 5;
    $firstDayTimestamp = mktime(0, 0, 0, $month, 1, $year);
    $firstDayWeekDay   = (int)date("N", $firstDayTimestamp); //1: monday, 7: saturday
    $upToDay           = (int)date("t", $firstDayTimestamp);

    $firstMonday = 1 === $firstDayWeekDay ? 1 : 9 - $firstDayWeekDay;
    $wholeWeeks  = $firstMonday < $upToDay ? (int)floor(($upToDay - $firstMonday + 1) / 7) : 0;
    $extraDays   = ($upToDay - $firstMonday + 1) % 7;

    $initialWorkdays      = $firstMonday > 1 && $firstDayWeekDay <= $workingWeekdays ? $workingWeekdays - $firstDayWeekDay + 1 : 0;
    $workdaysInWholeWeeks = $wholeWeeks * $workingWeekdays;
    $extraWorkdays        = $extraDays <= $workingWeekdays ? $extraDays : $workingWeekdays;

    return $initialWorkdays + $workdaysInWholeWeeks + $extraWorkdays;
}

Essas funções funcionam sem loops .

As funções calculam o número de dias da semana usando:

  • dia-número da primeira segunda-feira do mês
  • número de dias no mês
// main functions 
// weekdays in month of year
function calculateNumberOfWeekDaysAtDate($month, $year)
{
    // I'm sorry, I don't know the right format for the $month and $year, I hope this is right.
    // PLEASE CORRECT IF WRONG
    $firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of 01-$month-$year")); //get first monday in month for calculations
    $numberOfDaysOfCurrentMonth = (int) date("t", strtotime("01-$month-$year")); // number of days in month

    return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}

// week days in current month
function calculateNumberOfWeekDaysInCurrentMonth()
{
    $firstMondayInCurrentMonth = (int) date("j", strtotime("first monday of this month")); //get first monday in month for calculations
    $numberOfDaysOfCurrentMonth = (int) date("t"); // number of days in this month

    return calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth);
}

// helper functions
function calculateNumberOfWeekDaysFromFirstMondayAndNumberOfMonthDays($firstMondayInCurrentMonth, $numberOfDaysOfCurrentMonth)
{
    return $numberOfWeekDays = (($start = ($firstMondayInCurrentMonth - 3)) < 0 ? 0 : $start) + floor(($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) / 7) * 5 + (($rest = (($numberOfDaysOfCurrentMonth - ($firstMondayInCurrentMonth - 1)) % 7)) <= 5 ? $rest : 5);
}
function workingDays($m,$y) {
    $days = cal_days_in_month(CAL_GREGORIAN, $m, $y);
    $workig_days = 0;
    $days_rest = array(5,6); //friday,saturday
    for ( $d=1 ; $d < $days+1 ; $d++ ) {
        if ( !in_array(date("w",strtotime("{$d}-{$m}-{$y}")),$days_rest)  ) {
            $workig_days++;
        }
    }
    return $workig_days;
}

isso vai funcionar

// oct. 2013
$month = 10;

// loop through month days
for ($i = 1; $i <= 31; $i++) {

    // given month timestamp
    $timestamp = mktime(0, 0, 0, $month, $i, 2012);

    // to be sure we have not gone to the next month
    if (date("n", $timestamp) == $month) {

        // current day in the loop
        $day = date("N", $timestamp);

        // if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
        if ($day == 1 OR $day <= 5) {

            // write it down now
            $days[$day][] = date("j", $timestamp);
        }
    }
}

// to see if it works :)
print_r($days);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top