How to view and count the days of a week between two dates using javascript and php

Asked

Viewed 122 times

2

I have the general table tb_turg that stores the records like this:

idmodg: 13 (codigo do módulo)
codturg: 101 (código da turma)
iniclasg: 2020-01-13 (data de inicio)
terclasg: 2020-02-21 (data de termino)
weekg: 1,3,5 (dias da semana que tem curso, neste caso: segunda, quarta e sexta)


Each student has a table. Student id 1 has the table tb_z1 and stores the records like this:

idcur   | codtur    | diafrq
13  | 101       | 2020-01-13
13  | 101       | 2020-01-17
13  | 101       | 2020-01-20
13  | 101       | 2020-01-22
13  | 101       | 2020-01-29
13  | 101       | 2020-01-31
13  | 101       | 2020-02-03
13  | 101       | 2020-02-05
13  | 101       | 2020-02-07
etc...


How to calculate: How many days and what are the days of table tb_turg from start date (iniclasg) to end date (terclasg), but only on the days specified in the week (weekg), which would be

2020-01-13, 2020-01-15, 2020-01-17, 
2020-01-20, 2020-01-22, 2020-01-24, 
2020-01-27, 2020-01-29, 2020-01-31,
2020-02-03, 2020-02-05, 2020-02-07, 
2020-02-10, 2020-02-12, 2020-02-14,
2020-02-17, 2020-02-19, 2020-02-21,

18 days

How many attendance days did student id 1 09 DAYS (for example)

Once the data have been obtained, the following must be verified: How many FOUL days did student id 1?

I need to generate a report by passing the module code (13) and the class code (101) via GET. Can anyone help me? Thanks

EXIT: inserir a descrição da imagem aqui MY ATTEMPTS:

    date_default_timezone_set('America/Sao_Paulo');


$arrphp = $_GET['arrphp'];
$idmods = $_GET['idmods'];
$mod = $_GET['idmods'];     //----- Para escrever no SELECT do SQL
$codtur = $_GET['codtur'];
$tur = $_GET['turma'];
$prof = $_GET['prof'];

//if ($idmods) { include "../../sys/sqlmod.php"; }
//if ($mbkgmod == "#FFFFFF") { $borda = "border: 1px solid #ccc"; } else { $borda = ""; }

if ($tur == "A") { $nturma = "Turma 1"; $vog = "a"; } if ($tur == "B") { $nturma = "Turma 2"; $vog = "b"; }
if ($tur == "C") { $nturma = "Turma 3"; $vog = "c"; } if ($tur == "D") { $nturma = "Turma 4"; $vog = "d"; }


    $sqlturg = mysqli_query($conn, "SELECT codturg,profg,iniclasg,terclasg,weekg,classg FROM tb_turg WHERE codturg = '$codtur'");
    $rowtg = $sqlturg->fetch_assoc();
    $profg = $rowtg["profg"];
    $iniclasg = $rowtg["iniclasg"];
    $terclasg = $rowtg["terclasg"];
    $weekg = $rowtg["weekg"];
    $classg = $rowtg["classg"];

    if ($iniclasg) { $nwiniclasg = date('d/m/Y', strtotime($iniclasg)); } else { $nwiniclasg = ""; }
    if ($terclasg) { $nwterclasg = date('d/m/Y', strtotime($terclasg)); } else { $nwterclasg = "ABERTA"; }
    if ($nwterclasg == "ABERTA") { $calcnwter = date("d/m/Y"); } else { $calcnwter = $nwterclasg; }

    //if ($profg) { $mprofm = $profg; include "../../sys/sqlprof.php"; }

    if ($terclasg == "") { $nwtermino = date('Y-m-d'); }



?>

<?php //echo $_SERVER['REQUEST_URI']; ?>

<!doctype html>
<head>
<meta http-equiv="Content-Language" content="pt-br">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>
.tb-freq-def td {
    border-left:1px solid #000;
    border-bottom:1px solid #000;
}
</style>

</head>

<body>


<table class='tb-freq-def' id='table' cellspacing='0' cellpadding='0' style='width:calc(100% - 25px);border-top:1px solid #000;border-right:1px solid #000;'>
    <tr>
        <td width='182' colspan='2' align='center'>Alunos</td>
        <td width='599' align='center'>Frequências</td>
    </tr>
    <tr>

        <td width='182' colspan='2' align='center'><div class='ames'></div></td>

        <td width='599' align='left'>

        </td>


    </tr>



<?php
$sqlatva = "SELECT idalunos FROM tb_x$mod WHERE cdgtur = '$codtur'";        //ORDER BY idalunos ASC limit 1";
$queryma = $conn->query($sqlatva);
while($rowatva = $queryma->fetch_assoc()) {
$idaa = $rowatva["idalunos"];

//echo "<script>$('#$idaa').hide(0);</script>";

}
?>



<?php
$sqlatv = "SELECT idalunos,cdgtur FROM tb_x$mod WHERE cdgtur = '$codtur'";
$querym = $conn->query($sqlatv);
while($rowatv = $querym->fetch_assoc()) {
$ida = $rowatv["idalunos"];
?>


<?php
$sqlclitur = "SELECT idcli,idatv,nomei,generoi,fotocli FROM tb_cli WHERE idcli = '$ida'";
$queryb = $conn->query($sqlclitur);
while($rowctb = $queryb->fetch_assoc()) {
$cidcli = $rowctb["idcli"];
$cidatv = $rowctb["idatv"];
$cnomei = $rowctb["nomei"];
$cgeneroi = $rowctb["generoi"];
$cphoto = $rowctb["fotocli"];

if ( ($cgeneroi == "M") || ($cgeneroi == "") ) { $vgl = "o"; }
if ($cgeneroi == "F") { $vgl = "a"; }

$ursarra = explode(' ', $cnomei);
$nwcnomei = $ursarra[0];        //." ".$ursarra[1];

//----- Foto do Beneficiário
$codUserF = $cidcli;
$upload_dir = "data/photo/clients/";

$user_foto_prefix = "_ben.jpg?".date("YmdHis");
$user_atual_name = $codUserF.$user_foto_prefix;
$user_foto_location = $upload_dir.$user_atual_name;

$_dir = "../photo/clients/";
$_foto_prefix = "_ben.jpg";
$_atual_name = $codUserF.$_foto_prefix;
$_foto_location = $_dir.$_atual_name;

if ($cidatv == "X") {
$inatva = "<div class='emCursorP emRed3' onclick='benExclur(\"$cnomei|$vgl\");' style='position:absolute;width:100%;height:100%;-webkit-filter:blur(50px);z-index:1;'></div>";
$inatvf = "<div class='emCursorP' onclick='benExclur(\"$cnomei|$vgl\");' style='position:absolute;width:60px;height:60px;border-radius:100%;z-index:2;'></div>";
$inatvb = "display:none";
$inaticon = "<div class='emRed3 emWhtA emFont11' style='position:absolute;width:20px;height:20px;bottom:0;right:0;border-radius:100%;z-index:1;'><i class='fa fa-trash-alt'></i></div>";
} else {
$inatva = "";
$inatvf = "";
$inatvb = "";
$inaticon = "";
}

?>
    <tr>


<div style='position:relative;*width:100px;*margin-bottom:10px;'>
<!-- ===== INÍCIO FOTO BENEFICIÁRIO ===== -->
<div style='position:relative;width:40px;*border-radius:100%;'>

<?php echo $inaticon; ?>
<?php echo $inatvf; ?>


<?php if (file_exists($_foto_location)) { ?>
<div class='' style='position:relative;*border-radius:100%;'>
<div class='fotoe<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fotof<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fa fotog<?php echo $cidcli; ?>' style='position:absolute;line-height:1;z-index:2;'></div>
<?php $f_user = "<img class='foto<?php echo $cidcli; ?>' style='width:40px;*border-radius:100%;border:1px solid #f4f4f4;margin:0 auto;' src='$user_foto_location'>"; ?>
</div>
<?php } ?>
<?php if (!file_exists($_foto_location) && ($cgeneroi == 'M')) { ?>
<div class='' style='position:relative;*border-radius:100%;'>
<div class='fotoe<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fotof<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fa fotog<?php echo $cidcli; ?>' style='position:absolute;line-height:1;z-index:2;'></div>
<?php $f_user = "<img class=' foto<?php echo $cidcli; ?>' style='width:40px;*border-radius:100%;margin:0 auto;' src='data/photo/fotos/_m.png'>"; ?>
</div>
<?php } ?>
<?php if (!file_exists($_foto_location) && ($cgeneroi == 'F')) { ?>
<div class='' style='position:relative;*border-radius:100%;'>
<div class='fotoe<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fotof<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fa fotog<?php echo $cidcli; ?>' style='position:absolute;line-height:1;z-index:2;'></div>
<?php $f_user = "<img class=' foto<?php echo $cidcli; ?>' style='width:40px;*border-radius:100%;margin:0 auto;' src='data/photo/fotos/_f.png'>"; ?>
</div>
<?php } ?>
<?php if (!file_exists($_foto_location) && ($cgeneroi == '')) { ?>
<div class='' style='position:relative;*border-radius:100%;'>
<div class='fotoe<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fotof<?php echo $cidcli; ?> emMaiusc'></div>
<div class='fa fotog<?php echo $cidcli; ?>' style='position:absolute;line-height:1;z-index:2;'></div>
<?php $f_user = "<img class=' foto<?php echo $cidcli; ?>' style='width:40px;*border-radius:100%;margin:0 auto;' src='data/photo/fotos/_i.png'>"; ?>
</div>
<?php } ?>
</div>
<!-- ===== FINAL FOTO BENEFICIÁRIO ===== -->
</div>
<?php } ?>



        <td width='605' colspan='3'>

<div class='emLineB emTableCell emFont10' style='position:relative;width:71px;height:16px;font-weight:bold;'></div>

<?php
$days = explode(',', $arrphp);
foreach($days as $adddays) {
    $day = $adddays;


    $sqlday = mysqli_query($conn, "SELECT diafrq,codtur FROM tb_z$ida WHERE codtur = '$codtur'");
    $rowday = $sqlday->fetch_assoc();
    $diafrq = $rowday["diafrq"];

    if ($diafrq) {
    $arrfrq = explode('-', $diafrq);
    $nwdiafrq = $arrfrq[2];
    } else {
    $nwdiafrq = "";
    }

    if ($nwdiafrq == $day) { $nwcor = "emBlueA"; } else { $nwcor = "emRedA"; }  

if ( ($day) ) { $tbday = "<div class='$nwcor emLineB emTableCell emFont10' style='position:relative;width:18px;min-width:18px;height:16px;font-weight:bold;border-right:1px solid #ccc;'>" . $day . "</div>"; }
    echo $tbday;
}
?>



<?php

$sqlben = "SELECT idnrb,diafrq,diacur,frqcur,codtur FROM tb_z$ida WHERE codtur = '$codtur'";
$queryl = $conn->query($sqlben);


while($rowben = $queryl->fetch_assoc()) { 
$idnrb = $rowben["idnrb"];
$diafrq = $rowben["diafrq"];
$diacur = $rowben["diacur"];
$frqcur = $rowben["frqcur"];

if ($diafrq) {
$arrfrq = explode('-', $diafrq);
$nwdiafrq = $arrfrq[2];
} else {
$nwdiafrq = "";
}

//$freq = [ $f_user ." ". $nwcnomei  =>  [$nwdiafrq], ];



?>




<?php } ?>


</td>
    </tr>

<?php } ?>

</table>

But I’m not getting the dates of the week to do the accounting of how many frequencies and how many absences.

DEU CERTO COM A AJUDA DO NOSSO AMIGO FURLAN

That is the result: inserir a descrição da imagem aqui

1 answer

0


Hello. I don’t know if that was your question, but see if the excerpt below helps you.

According to https://www.php.net/manual/en/function.date.php, the function format('w') is the numerical representation of the day of the week, being from 0 (for Sunday) until 6 (for Saturday).

$iniclasg = new DateTime('2020-01-13');
$terclasg = new DateTime('2020-02-21');

$dias_que_tem_aula = array();

while($iniclasg <= $terclasg){

    //Verifica quais dias dentro desse intervalo são segunda, quarta e sexta
    if($iniclasg->format('w') == 1 || $iniclasg->format('w') == 3 || $iniclasg->format('w') == 5){

        //Caso seja 1, 3 ou 5, adiciona a data ao array
        $dias_que_tem_aula[] = $iniclasg->format('Y-m-d');

    }

    //adiciona um dia à data inicial para seguir verificando a partir da próxima data
    $iniclasg = $iniclasg->modify('+1day');
}

//aqui é o resultado da tabela tb_z1, coluna diafrq
$diasfrq = array('2020-01-13', '2020-01-17', '2020-01-20', '2020-01-22', '2020-01-29', '2020-01-31', '2020-02-03', '2020-02-05', '2020-02-07', '2020-02-21');

$dias_presentes = count($diasfrq);
$dias_ausentes = (count($dias_que_tem_aula) - $dias_presentes);


echo $dias_presentes;
echo '<br>';
echo $dias_ausentes;

Don’t judge me, because I know that there are likely to be more performative ways. However, I believe it will give you a good idea of how it works to implement your own solution.

Note: All dates are being used in the form 'Y-m-d'. Therefore, you will have to make changes if you are using the form’d-m-Y'.

  • My friend, thank you so much for this... may God enlighten you! But my problem is to put this: if($iniclasg->format('w') == 1 || $iniclasg->format('w') == 3 || $iniclasg->format('w') == 5) in a more dynamic way... this is exactly where I’m in trouble. Example: Course A, can have the days 1,3,5 or 2,4 etc... The weekg column keeps the days of the week just like this (number with comma). How to open the dynamic report with the same code: Course A - Class 1 - Days 1,3,5 or Course A - Class 2 - Days 2,4 or Course A - Class 3 - Days 1,5. Please help me out

  • I did so and it seems to be working: $days = explode(',', $weekg); foreach($days as $day) { $b = $day; if($novoinic->format('w') == $b) { - What do you think @Leandro Furlan? Dude I want to tell you that you saved "my life"... God enlighten you!

  • That’s right. The explode function will separate all numbers from the string. It will get a foreach within a while and a few if within the foreach, but I believe that if the period and number of days of the week are not very extensive, there won’t be so much loss of performance. Glad I could help somehow.

  • Man worked... I got thanks the size of the galaxy! May God enlighten you!

Browser other questions tagged

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