PHP 数据导出为 csv - 只提供旧数据的结果

问题描述 投票:0回答:0

我有一个表格,用户可以选择 A)。选择日期范围并导出该范围内的数据或 B)。导出所有数据(使用复选框)。

导出所有数据都完美无缺,但是如果我使用日期范围查询,它就会出现问题。开发数据库数据一直持续到 2021 年底。如果我选择 2021 年的日期范围,它不会导出任何内容,但是如果我为 2018 年至 2021 年的某些日期做一个较早的日期范围,它就可以正常工作。

StartTime
StopTime
列包含 UNIX 时间戳。

我不知道我在这里做错了什么......

这是PHP

if ($action == "exportpireps")
    {
        if ($id != "")
        {
            //convert inputted times to unix time stamp
            $startDate = strtotime($_POST['flight-data-start']);
            $endDate = strtotime($_POST['flight-data-end']);

            if (!isset($_POST['flight-data-all'])) {
                // Query the database for flights in date range
                $sql = "SELECT DepICAO, DesICAO, StartTime, Duration, Callsign, AC, StopTime, Distance FROM {$dbprefix}Reports WHERE PilotID=$id AND StartTime BETWEEN $startDate AND $endDate ORDER BY StopTime";
            }
            else
            {
                // Query the database for all flights
                $sql = "SELECT DepICAO, DesICAO, StartTime, Duration, Callsign, AC, StopTime, Distance FROM {$dbprefix}Reports WHERE PilotID=$id ORDER BY StopTime";
            }

            $result = $con->query($sql);

            // Generate CSV file
            $filename = "pc_flight_reports.csv";
            $fp = fopen('php://output', 'w');

            // Set headers for CSV download
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename="'.$filename.'"');

            // Write the column headers to the CSV file
            fputcsv($fp, array('Origin', 'Destination', 'DepartureTime', 'Duration', 'Callsign', 'AircraftType', 'ArrivalTime', 'Distance'));

            // Write each row of data to the CSV file
            while ($row = $result->fetch_assoc()) {
                fputcsv($fp, $row);
            }

            // Close the file pointer
            fclose($fp);

        }

    }

这是表单的 HTML/JS。

<div class="alert alert-info" role="alert">This tool will export your PIREP data as a CSV file.</div>
                <form action="<?php echo $ipsactionsurl ?>action.php?action=exportpireps&id=<?php echo $pilotid ?>" method="post" enctype="application/x-www-form-urlencoded">
                    <div class="form-row">
                        <div class="col">
                            <label for="flightDataStart">Start date:</label>
                            <input class="form-control" type="date" id="flightDataStart" name="flight-data-start" min="2018-01-01" max="2024-12-31">
                        </div>
                        <div class="col">
                            <label for="flightDataEnd">End date:</label>
                            <input class="form-control" type="date" id="flightDataEnd" name="flight-data-end" min="2018-01-01" max="2024-12-31">
                        </div>
                    </div>
                    <div class="form-row mt-2 ml-3">
                        <div class="col">
                            <input type="checkbox" class="form-check-input" id="allFlightsCheck" name="flight-data-all">
                            <label class="form-check-label" for="allFlightsCheck">Include All Flights</label>
                        </div>
                    </div>
                    <div class="form-row mt-2">
                        <div class="col">
                            <input type="submit" class="btn btn-primary" value="Submit" />
                        </div>
                    </div>
                    <script>
                    document.getElementById('allFlightsCheck').onchange = function() {
                        document.getElementById('flightDataStart').disabled = this.checked;
                        document.getElementById('flightDataEnd').disabled = this.checked;
                    };
                </script>
                </form>

我试过在查询中使用

StartTime >= xxx AND StartTime <= xxx
但没有用。

php mysql export-to-csv unix-timestamp
© www.soinside.com 2019 - 2024. All rights reserved.