【问题标题】:MYSQL not returning results between dates are in different yearsMYSQL 不返回日期之间的结果是在不同的年份
【发布时间】:2014-01-28 10:32:51
【问题描述】:

我正在尝试查找介于 2 个日期之间的记录。我的日期设置在 2 个 php 变量中,即今天的日期,然后是今天之前的 8 周。该代码在2个变量在同一年内时有效,但是当日期在上一年时,找不到结果;即 $today = 2014 年 1 月 28 日和 $lastmonth = 2013 年 12 月 3 日。当我查看日期结果时,这些变量似乎计算正确。是什么导致了问题?

<?php
$today = date('Y-m-d');
$lastmonth = date('Y-m-d', strtotime('-8 week', strtotime($today)));

$sql = mysql_query("SELECT * FROM products WHERE dated_release BETWEEN '$lastmonth' AND '$today' ORDER BY code");

$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount > 0) { // evaluate the count and build display
// more code

?>

表中的 dated_release 字段已设置为日期字段。这是我第一个使用 php 和 mysql 的网站。

【问题讨论】:

    标签: php mysql date


    【解决方案1】:

    你可以像这样创建一个简单的查询

    SELECT *
    FROM products
    WHERE dated_release BETWEEN NOW()
        AND DATE_ADD(NOW(), INTERVAL -8 WEEK);
    

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      相关资源
      最近更新 更多