Logic - Knowing if you are on the second day of the month

Asked

Viewed 155 times

0

I’m having difficulty formulating a logic to know whether or not the day I’m on is for example the second Saturday of the month of February. With the code I made I can take the day I am, month and year, plus go through each day of the month and stop the execution of while if the total of days of the month.

Code developed:

    date_default_timezone_set('America/Sao_Paulo');

    $diasSemanaSigla = array('Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb' );

    $date = date('d-m-Y');

    //echo "Data: ".$date."\n";

    $data = explode('-', $date);
    $dia = $data[0];
    $mes = $data[1];
    $ano = $data[2];

    //echo 'dia: '.$dia.' mes: '.$mes.' ano: '.$ano."\n";

    //echo date( 'w', mktime( 0, 0, 0, $mes, $dia, $ano) )."\n";

    $mes = $mes == null ? (int) date('m') : (int) $mes;
    //echo "variavel mes: ".$mes."\n";
    $ano = $ano == null ? date('Y') : $ano;
    //echo "variavel ano: ".$ano."\n";

    $qtdDiasMes = cal_days_in_month( CAL_GREGORIAN, $mes, $ano);
    //echo "Quantidade dias mes: ".$qtdDiasMes."\n";

    $diasVaziosInicio = date( 'w', mktime( 0, 0, 0, $mes, $dia, $ano) );
    //echo "Dias Vazios inicio: ".$diasVaziosInicio."\n";
    $totalDias = 1;
    while( $qtdDiasMes >= $totalDias ) {
        for( $i = 1; $i <= 7; $i++) {
            if( ($totalDias > $qtdDiasMes)  ) {
                echo "entrou primeiro if"."\n";
                echo "Total dias: ".$totalDias."\n";
                echo "qtdDiasMes: ".$qtdDiasMes."\n";
            } elseif( $diasVaziosInicio != 0 ) {
                echo "entrou segundo if"."\n";
                echo "Dias vazios: ".$diasVaziosInicio."\n";
                $diasVaziosInicio--;
            } else {
                echo "entrou else"."\n";
                echo "Total dias:".$totalDias."\n";
                $totalDias++;
            }
        }
    }

2 answers

1


From the day of the month (date('j')), you can find out if it is the 1st, 2nd, ..., nº (Monday to Saturday) day of the month. For this it is necessary to divide the day of the month by 7 (to see if completed one week). The code for this would be:

<?php

date_default_timezone_set('America/Sao_Paulo');

$ordem = date('j');

//ceil arredonda uma fração para o maior inteiro mais próximo
//date('D') representa o dia da semana (Mon,Tur,thu,wed,fri,sat,sun)
echo ceil($ordem / 7) . 'º ' . date('D');

When dividing the variable $ordem by 7, obtain a fractional number that when rounded up represents the agenda of the week in the month (if it is 1º, 2º, etc.).

1

//primeiro sábado de fevereiro de 2018
echo date('d-m-Y', strtotime('first saturday of february 2018'));
//segundo sábado de fevereiro de 2018
echo date('d-m-Y', strtotime('second saturday of february 2018'));
//terceiro 
echo date('d-m-Y', strtotime('third saturday of february 2018'));
//quarto 
echo date('d-m-Y', strtotime('fourth saturday of february 2018'));

//quinto sábado - se não houver passa para o próximo mes 
echo date('d-m-Y', strtotime('fifth saturday of february 2018')); // 03-03-2018

example - ideone

General date('d-m-Y', strtotime('ORDINAL DIA of MES ANO'));

To find out if the day I am is or is not the second Saturday of the month of February

$segundosabado = date('d-m-Y', strtotime('second saturday of february 2018'));

$hoje = date('d-m-Y');

echo ($hoje == $segundosabado) ? "é" : "não é";

Browser other questions tagged

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