0

这是我的事件脚本,用于提取接下来 7 天的约会,它似乎工作正常,但仅在一种情况下........日期和时间以日期时间格式保存在 mysql 数据库中,所以 2013- 12-23 08:30:00。我的脚本每天都会打印出来,并为要下车或取货的客户找到当天的约会。mysql 查看数据库并将客户与下车或取货字段匹配到打印日期,并将它们添加到日期下方的 div 中。

我遇到的问题是,如果时间设置为 00:00:00 以外的任何时间,则当天不会接听该客户。如何让比较忽略时间而只使用日期?

            // Date box container
    echo '<div class="dateboxcontainer">';

    // Loop through and create a date for the next 7 days
    $days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7); 
    foreach ($days as $day) {
    echo '<div class="datebox">';
    echo '<div class="topdate">';

          echo strtoupper($day->format('D d')) . PHP_EOL;
    echo '</div>';  

              // Get the names for each day
          $theday = strtoupper($day->format('Y-m-d'));    
          $sqldate = <<<SQL
        SELECT *
        FROM `jobdetails`
        WHERE datedroppingoff = '$theday' OR datepickingup = '$theday'
    SQL;
    if(!$resultdate = $db->query($sqldate)){
        die('There was an error running the query [' . $db->error . ']');
    }
    while($rowdate = $resultdate->fetch_assoc()){
        echo $rowdate['name'];
            }
         //

    echo '</div>';
    }  
    echo '</div>';
    //
4

2 回答 2

1

您现在正在做的是将日期/时间值与日期值进行比较。如果时间部分不是午夜,则此比较将失败。

您可以通过使用DATE()MySql 函数将苹果与苹果进行比较来修复比较:

WHERE DATE(datedroppingoff) = '$theday' OR DATE(datepickingup) = '$theday'

还有其他方法可以做到这一点,例如

WHERE DATEDIFF(datedroppingoff, '$theday') = 0 OR ...

如果你$nextday手头有一个价值,你也可以做

WHERE (datedroppingoff >= '$theday' AND datedroppingoff < '$nextday') OR ...
于 2013-07-23T09:26:54.600 回答
0

您在 mySQL 中存储特定的时间和日期,但仅在 SQL 查询中搜索日期。由于 mySQL 不了解您想要搜索完整的一天还是特定时间点之间的区别,因此 mySQL 假定您在时间 0:00:00 查找当天。

你有几个选择,你可以搜索一个时间段(伪代码,自己检查边界):

WHERE datedroppingoff > '$theday' AND datedroppingoff < '$theday'+1

另一种选择是将日期和时间存储在单独的数据库字段中。这样你就可以让你的 SQL 查询更简单。

祝你好运。

于 2013-07-23T09:32:03.090 回答