php 检查当前日期是否在两个日期之间 + mysql select 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13563423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if the current date is between two dates + mysql select query
提问by J.K.A.
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 dateStartand dateEnd.
我想检查今天的日期是否在dateStart和之间dateEnd。
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.
但到目前为止它不起作用,因为它返回零行。
回答by Sashi Kant
Try this :: This will solve your problem
试试这个 :: 这将解决你的问题
SELECT * FROM `table` WHERE active=0 AND CURDATE() between dateStart and dateEnd
回答by pritaeas
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.
并且您需要在查询中用单引号将其括起来。
回答by Amit Garg
$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;
回答by Amit Garg
$sql = mysql_query("SELECT * FROM register WHERE CURDATE() between reg_start_date and reg_end_date") or die(mysql_error());
回答by user2932719
You needed to put single quotes around the changed date:
您需要在更改的日期周围加上单引号:
"SELECT * FROM `table` WHERE active=0 AND '{$todaysDate}' between dateStart and dateEnd"
回答by Jebaraj Bernad Shaw
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());

