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

时间:2023-02-08 08:35:16

I have 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 compare todays date in between dateStart and dateEnd.

我想在dateStart和dateEnd之间比较今天的日期。

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 its not working. Any solution. Thanks in advance.

但它不起作用。任何解决方案提前致谢。

5 个解决方案

#1


26  

Try this :: This will solve your problem

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

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

#2


2  

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.

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

#3


0  

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

#4


0  

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

#5


0  

You needed to put single quotes around the changed date:

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

"SELECT * FROM `table` WHERE active=0 AND '{$todaysDate}' between dateStart and dateEnd"

#1


26  

Try this :: This will solve your problem

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

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

#2


2  

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.

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

#3


0  

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

#4


0  

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

#5


0  

You needed to put single quotes around the changed date:

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

"SELECT * FROM `table` WHERE active=0 AND '{$todaysDate}' between dateStart and dateEnd"