<?php
// Database Connection
include "includes/db/db.php";
$query = mysqli_query($connection,"SELECT * FROM team_attendance JOIN team_login ON
team_attendance.attendance_user_id=team_login.user_id where
attendance_activity_name='Checked-In' order by
team_attendance.attendance_id ASC"); // Get data from Database from
demo table
$delimiter = ",";
$filename = "attendance" . date('Ymd') . ".csv"; // Create file name
//create a file pointer
$f = fopen('php://memory', 'w');
//set column headers
$fields = array('ID', 'Employee Name', 'Check In Time', 'Check Out Time', 'Date');
fputcsv($f, $fields, $delimiter);
//output each row of the data, format line as csv and write to file pointer
while($row = $query->fetch_assoc()){
$date=date('d-m-Y',$row['attendance_date']);
$lineData = array($row['attendance_id'], $row['user_name'], $row['attendance_time'],
$row['check_out_time'],$date);
fputcsv($f, $lineData, $delimiter);
}
//move back to beginning of file
fseek($f, 0);
//set headers to download file rather than displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
//output all remaining data on a file pointer
fpassthru($f);
?>