<?php
session_start();
$server = "localhost";
                        $username="root";
                        $password="";
                        $dbname="motox";

$conn = new mysqli($server,$username,$password,$dbname);

if($conn->connect_error){
    die("Connection failed" .$conn->connect_error);
}

if(isset($_POST['stdate']) ) {

    $det1 = $_POST['stdate'];
    $det2 = $_POST['etdate'];
    $d1 = DATE($det1);
    $d2 = DATE($det2);
    $filename = 'AttendanceRecord-'.date('Y-m-d').'.xls';

    $query = "SELECT EMPID,SUBSTRING(QUOTE(DATE_FORMAT(LOGDATE, '%Y-%m-%d %T')),1,CHAR_LENGTH(QUOTE(DATE_FORMAT(LOGDATE, '%Y-%m-%d %T'))) -1) AS LOGDATE,DEVICENO,DEVICENAME,STATUS FROM qrattendance  WHERE DATE(LOGDATE) BETWEEN '$d1' AND '$d2'";
    $result = mysqli_query($conn,$query);
    
    $array = array();
    
    $file = fopen($filename,"w");
    $array = array("EMPID","LOGDATE","DEVICENO","DEVICENAME","STATUS");
    fputcsv($file,$array);
    
    while($row = mysqli_fetch_array($result)){
        $STUDENTID =$row['EMPID'];
        $TIMEIN =STR_REPLACE("'", " " , $row['LOGDATE']);
        $TIMEOUT =$row['DEVICENO'];
        $LOGDATE =$row['DEVICENAME'];
        $STATUS =$row['STATUS'];
    
        $array = array($STUDENTID,$TIMEIN,$TIMEOUT,$LOGDATE,$STATUS);
        fputcsv($file,$array);
    }
    fclose($file);
    
    header("Content-Description: File Transfer");
    header("Content-Disposition: Attachment; filename=$filename");
    header("Content-type: application/xls;");
    readfile($filename);
    unlink($filename);
    exit();

    

}





?>