库存报表Date Range功能异常求助:选日期区间时结束日期未显示
Hey there, let's dig into why your inventory report's date range picker is missing March 8th data when you select 2018-03-07 to 2018-03-08—especially since it works fine for other reports. Based on common pitfalls with date range logic, here are the most likely fixes to check:
1. Check Your SQL Date Filter Logic (Most Common Culprit)
The #1 reason for missing end-date data is a mismatch between how your database stores dates (often with timestamps) and how your query filters them. For example:
- If your
inventorytable uses adatetimefield (e.g.,2018-03-08 14:25:00), a query like this will exclude all March 8th data after midnight:
Because$sql = "SELECT * FROM inventory WHERE date BETWEEN '$start_date' AND '$end_date'";'2018-03-08'gets interpreted as'2018-03-08 00:00:00', so any records later that day get cut off.
Fix it by adjusting the query to cover the full end date:
Option 1 (preserves index usage, better performance):
$sql = "SELECT * FROM inventory WHERE date >= '$start_date 00:00:00' AND date <= '$end_date 23:59:59'";
Option 2 (simpler, but may ignore indexes on the date field):
$sql = "SELECT * FROM inventory WHERE DATE(date) BETWEEN '$start_date' AND '$end_date'";
2. Verify Datepicker Format Matches Your Database
Your current code initializes the datepicker but doesn’t set a format. If your database stores dates as YYYY-MM-DD but the datepicker outputs MM/DD/YYYY, it can cause silent parsing errors that break the end date filter.
Add a format to your datepicker initialization:
$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' // Ensures output matches database's date format });
3. Check for Inventory-Report-Specific Filters
Since other reports work, your inventory report might have extra logic that accidentally excludes end-date data. Look for:
- Hardcoded time restrictions (e.g., only including records before the end date’s midnight)
- Additional status filters (e.g., only showing "completed" inventory movements that might not include March 8th entries)
- Post-query filtering in PHP (e.g., looping through results and skipping records from the end date)
4. Debug the Actual Parameters Being Passed
Add quick debug lines to your PHP code to confirm the date values are being received correctly:
// Print these values to check if dates are passed properly echo "Start Date Received: " . $_POST['start_date'] . "<br>"; echo "End Date Received: " . $_POST['end_date'] . "<br>"; echo "Final SQL Query: " . $sql;
This will help you rule out typos or incorrect date parsing in the request.
If you can share a more complete snippet of your inventory report’s PHP/SQL code (especially the date handling parts), we can narrow this down even further!
内容的提问来源于stack exchange,提问作者user6914946

