简体   繁体   English

检查当前日期是否在两个日期之间+ mysql select查询

[英]Check if the current date is between two dates + mysql select query

I have the following table :我有下表:

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

I want to check if today's date is in between dateStart and dateEnd .我想检查今天的日期是否在dateStartdateEnd之间。

The following is my query for this :以下是我对此的查询:

$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;

But so far it's not working as it returns zero rows.但到目前为止它不起作用,因为它返回零行。

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

SELECT * FROM `table` WHERE active=0 AND CURDATE() between dateStart and dateEnd

MySQL uses YYYY-MM-DD by default: MySQL 默认使用 YYYY-MM-DD:

$todaysDate="2012-26-11";

should be:应该:

$todaysDate="2012-11-26";

And you need single quotes around it in the query.并且您需要在查询中用单引号将其括起来。

$sql = mysql_query("SELECT * FROM register WHERE CURDATE() between reg_start_date and reg_end_date") or die(mysql_error());

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

"SELECT * FROM `table` WHERE active=0 AND '{$todaysDate}' between dateStart and dateEnd"
   $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`');
            

Do like this use ""这样做使用“”

$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;
$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;  

Try removing the {} as described below.尝试删除 {},如下所述。

$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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM