我有一个PHP表单,它以下列格式提交数据:
Array (
[zone] => Array (
[0] => East
)
[publication] => Array (
[0] => ABP
)
[datefilter] => 09/01/2016 - 10/15/2016
)
我使用数据构建MySQL查询,如下所示:
$zone_search = $_POST['zone'];
$zone = join("','",$zone_search);
$publication_search = $_POST['publication'];
$publication = join("','",$publication_search);
$tmp = "SELECT * FROM advertisement
WHERE zone IN('$zone')
AND publication IN('$publication')";
但是,我想将数据限制为$_POST['datefilter']
字段中给出的两个日期。如何将日期过滤日期添加到我的查询中?
数据库表中的相关列是:
zone
publication
release_date
答案 0 :(得分:1)
在发布数据后添加以下逻辑。假设日期数据提交遵循此模式小日期 - 根据样本发布数据的大日期。
<?
//$_POST['datefilter'] = '09/01/2016 - 10/15/2016';
$zone_search = $_POST['zone'];
$zone = join("','",$zone_search);
$publication_search = $_POST['publication'];
$publication = join("','",$publication_search);
$dates = explode("-", $_POST['datefilter']);
echo $dt1 = date('Y-m-d', strtotime(trim($dates[0])));
echo "<br/>";
echo $dt2 = date('Y-m-d', strtotime(trim($dates[1])));
$tmp = "SELECT * FROM advertisement
WHERE zone IN('$zone')
AND publication IN('$publication') and release_date between $dt1 and $d2";
答案 1 :(得分:-2)
您可以尝试使用php-function strtotime()
将日期转换为时间戳。您还需要将数据库中的列更改为“timestamp”。然后你可以比较两个更大和更小的整数。
例如SELECT * FROM advertisement WHERE zone IN ('$zone') AND publication IN ('$publication') AND release_date > ('$timestamp_from') AND release_date < ('$timestamp_to')