<?php
//Connect to database
require'connectDB.php';

$output = '';

if(isset($_POST["To_Excel"])){
  
    if ( empty($_POST['date_sel'])) {

        $Log_date = date("Y-m-d");
    }
    else if ( !empty($_POST['date_sel'])) {

        $Log_date = $_POST['date_sel']; 
    }

        $sql = "SELECT *,TIME(tgl1) as waktuin,TIME(tgl2) as waktuot FROM data_logs WHERE tgl='$Log_date' ORDER BY id DESC";
        $result = mysqli_query($conn, $sql);
        if($result->num_rows > 0){
            $output .= '
                        <table class="table" bordered="1">  
                          <TR>
          <th>Nama</th>
          <th>Rfid</th>
          <th>Tanggal</th>
          <th>Waktu Masuk</th>
          <th>Waktu Keluar</th>
 
          
          
                          </TR>';
              while($row=$result->fetch_assoc()) {
                  $output .= '
                              <TR> 
                
                                  <TD> '.$row['username1'].'</TD>
                                  <TD> '.$row['idrfid'].'</TD>
                                  <TD> '.$row['tgl1'].'</TD>
                                  <TD> '.$row['waktuin'].'</TD>
                                  <TD> '.$row['waktuot'].'</TD>

                              </TR>';
              }

                  
              $output .= '</table>';
              header('Content-Type: application/xls');
              header('Content-Disposition: attachment; filename=User_Log'.$Log_date.'.xls');
              
              echo $output;
              exit();
        }
        else{
            header( "location: UsersLog.php" );
            exit();
        }
}
?>