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++;
}
}
}