48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
include "../connect.php";
|
|
$current_month = date('m');
|
|
$current_year = date('Y');
|
|
// Get the first and last days of the current month.
|
|
$first_day_of_month = date('Y-m-d', strtotime($current_year . '-' . $current_month . '-01'));
|
|
$last_day_of_month = date('Y-m-d', strtotime($current_year . '-' . $current_month . '-' . cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year)));
|
|
|
|
// Create a SQL query to select the total passengers for each day in the current month.
|
|
$sql = "
|
|
WITH RECURSIVE date_series AS (
|
|
SELECT '$first_day_of_month' AS date
|
|
UNION ALL
|
|
SELECT DATE_ADD(date, INTERVAL 1 DAY)
|
|
FROM date_series
|
|
WHERE date < LEAST(CURDATE(), DATE_SUB('$last_day_of_month', INTERVAL 1 DAY))
|
|
)
|
|
SELECT
|
|
date_series.date AS day,
|
|
COALESCE(COUNT(passengers.id), 0) AS totalPassengers,
|
|
(SELECT COUNT(*) FROM passengers
|
|
WHERE passengers.created_at >= '$first_day_of_month'
|
|
AND passengers.created_at < '$last_day_of_month') AS totalMonthly
|
|
FROM
|
|
date_series
|
|
LEFT JOIN
|
|
passengers ON DATE(passengers.created_at) = date_series.date
|
|
WHERE
|
|
date_series.date >= '$first_day_of_month'
|
|
AND date_series.date < '$last_day_of_month'
|
|
GROUP BY
|
|
date_series.date
|
|
ORDER BY
|
|
date_series.date DESC
|
|
";
|
|
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute();
|
|
$passenger_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($passenger_data) {
|
|
// Print the passenger data as JSON
|
|
printSuccess($data = $passenger_data);
|
|
} else {
|
|
// Print a failure message
|
|
printFailure($message = "No passenger data found");
|
|
}
|
|
?>
|