使用mysql,php检索基于日期范围的数据

时间:2013-10-21 11:28:54

标签: php mysql

我在WPF上工作,当我尝试检索日期范围内的信息时,我有两个日期选择器,它在所有日期只显示一条记录(同一记录显示多次,例如:日期从2013年10月1日开始 - 3 / 10/2013)我每天有3个不同的记录,但我的输出是第一个显示3次相同日期和时间的记录。

function cpWhitelistStats() {
    $startDate = $_POST['startDate'];
    $startDateTime = "$startDate 00:00:00";
    $endDate = $_POST['endDate'];
    $endDateTime = "$endDate 23:59:59";
    $cpId = $_POST['id'];
    $cpName = etCommonCpNameById($cpId);

    print "<h2 style=\"text-align: center;\">Permitted Vehicle Summary</h2>";
    print "<h2 style=\"text-align: center;\">for $cpName</h2>";
    $tmpDate = explode("/", $startDate);
    $startYear = $tmpDate[2];
    $startMonth= $tmpDate[1];
    $startDay = $tmpDate[0];
    $tmpDate = explode("/", $endDate);
    $endYear = $tmpDate[2];
    $endMonth= $tmpDate[1];
    $endDay = $tmpDate[0];

    $startDateTime = "$startYear-$startMonth-$startDay 00:00:00";
    $endDateTime = "$endYear-$endMonth-$endDay 23:59:59";

    $custId = $_SESSION['customerID'];
    $realCustomerId = $_SESSION['realCustomerId'];

    $maxVal = 0;

    if ($custId != "") {
        $conn = &newEtConn($custId);
        // Get the whitelist plates
        $staticWhitelistArray = etCommonMkWhitelist($conn, $cpId);
        array_shift($staticWhitelistArray);

        $startLoopDate = strtotime($startDateTime);
        $endLoopDate = strtotime($endDateTime);
        $oneDay = 60 * 60 * 24;

        // Get the entries
        $plateList = array_keys($staticWhitelistArray);
        $plate_lookup = implode('","', $plateList);
        $sql = "SELECT plate, entry_datetime, exit_datetime FROM stats WHERE plate IN (\"$plate_lookup\") AND entry_datetime > \"$startDateTime\" AND entry_datetime < \"$endDateTime\" AND carpark_id=\"$cpId\" ";
        $result = $conn->Execute($sql);
        if (!$result) {
            print $conn->ErrorMsg();
            exit;
        }
        $rows = $result->fields;
        if ($rows != "") {
            unset($myArray);
            foreach($result as $values) {
                $plate = $values['plate'];
                $new_platelist[] = $plate;
                $inDateTime = $values['entry_datetime'];
                $outDateTime = $values['exit_datetime'];
                $tmp = explode(' ', $inDateTime);
                $inDate = $tmp[0];
                $in_ts = strtotime($inDateTime);
                $out_ts = strtotime($outDateTime);
                $duration = $out_ts - $in_ts;
                $dur_array = intToDateArray($duration);
                $dur_string = '';
                if ($dur_array['days'] > 0) {
                    $dur_string .= $dur_array['days'] . ' days ';
                }
                if ($dur_array['hours'] > 0) {
                    $dur_string .= $dur_array['hours'] . ' hours ';
                }
                if ($dur_array['mins'] > 0) {
                    $dur_string .= $dur_array['mins'] . ' minutes ';
                }
                if ($dur_array['secs'] > 0) {
                    $dur_string .= $dur_array['secs'] . ' secs ';
                }
                $myArray[$plate][] = array($inDateTime, $outDateTime, $inDate, $dur_string);
            }
        }

        while ($startLoopDate < $endLoopDate) {
            $dayString = strftime("%a, %d %B %Y", $startLoopDate);
            $dayCheck = strftime("%Y-%m-%d", $startLoopDate);

            print "<h2>$dayString</h2>";
            print "<table width=\"100%\">";
            print "  <tr>";
            print "    <th>VRM</th>";
            print "    <th>Permit Group</th>";
            print "    <th>Entry Time</th>";
            print "    <th>Exit Time</th>";
            print "    <th>Duration</th>";
            print "  </tr>";
            foreach($new_platelist as $wlPlate) {
                if ($myArray[$wlPlate][0][2] == $dayCheck) { 
                print "<tr>";
                print "<td>$wlPlate</td>";
                if (isset($myArray[$wlPlate])) {
                    print "<td>".$staticWhitelistArray[$wlPlate]['groupname']."</td>";
                    print "<td>".$myArray[$wlPlate][0][0]."</td>";
                    print "<td>".$myArray[$wlPlate][0][1]."</td>";
                    print "<td>".$myArray[$wlPlate][0][3]."</td>";
                }
                else {
                    print "<td>Vehicle Not Seen</td>";
                    print "<td>Vehicle Not Seen</td>";
                    print "<td>Vehicle Not Seen</td>";
                }
                print "</tr>";
                }
            }
            print "</table>";
            $startLoopDate = $startLoopDate + $oneDay;
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

您能否回复此查询,以便我们可以看到实际发送的内容......

$sql = "
SELECT plate
     , entry_datetime
     , exit_datetime 
  FROM stats 
 WHERE plate IN ('$plate_lookup') 
   AND entry_datetime BETWEEN '$startDateTime' AND '$endDateTime'
   AND carpark_id= '$cpId';
   ";

(这不是答案,但我想使用其他格式化选项)