18

我有下表:

id     dateStart     dateEnd      active
1      2012-11-12    2012-12-31   0
2      2012-11-12    2012-12-31   0

我想检查今天的日期是否介于dateStart和之间dateEnd

以下是我对此的查询:

$todaysDate="2012-26-11";
$db = Zend_Registry::get("db");
$result = $db->fetchAll("SELECT * FROM `table` WHERE active=0 AND {$todaysDate} between dateStart and dateEnd");
return $result;

但到目前为止它不起作用,因为它返回零行。

4

7 回答 7

38

试试这个 :: 这会解决你的问题

SELECT * FROM `table` WHERE active=0 AND CURDATE() between dateStart and dateEnd
于 2012-11-26T11:02:12.697 回答
4

MySQL 默认使用 YYYY-MM-DD:

$todaysDate="2012-26-11";

应该:

$todaysDate="2012-11-26";

并且您需要在查询中使用单引号。

于 2012-11-26T10:59:42.587 回答
0
$sql = mysql_query("SELECT * FROM register WHERE CURDATE() between reg_start_date and reg_end_date") or die(mysql_error());
于 2013-08-30T14:06:29.930 回答
0

您需要在更改的日期周围加上单引号:

"SELECT * FROM `table` WHERE active=0 AND '{$todaysDate}' between dateStart and dateEnd"
于 2016-05-11T15:31:18.623 回答
0
   $date=date("m/d/Y");
   $data=$this->Comman_model->select_manuvl('select * from company_news_details where userid='.$_SESSION['user_key'].' 
     AND "'.$date.'" between `rdate` 
     AND `edate`');
            
于 2021-10-22T10:45:14.713 回答
-1
$todaysDate="2012-11-26";//changed date
$db = Zend_Registry::get("db");
$select=$db->query("SELECT * FROM `table` WHERE active=0 AND {$todaysDate} between dateStart and dateEnd");
$result = $select->fetchAll();
return $result;  
于 2012-11-26T11:04:30.753 回答
-1

尝试按如下所述移除 {}。

$todaysDate="2012-11-26";//changed date
$sql = mysql_query("SELECT * FROM register WHERE '$todaysDate' between reg_start_date and reg_end_date") or die(mysql_error());
于 2020-02-14T08:10:21.187 回答