2016-09-16 64 views
0

我有按以下格式提交數據的PHP形式選擇MySQL的日期範圍:如何使用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 

回答

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";
-2

您可以嘗試的日期轉換爲使用PHP函數strtotime()時間戳。您還需要將數據庫中的列更改爲「時間戳」。然後你可以比較兩個大小不等的整數。 例如SELECT * FROM advertisement WHERE zone IN ('$zone') AND publication IN ('$publication') AND release_date > ('$timestamp_from') AND release_date < ('$timestamp_to')

相關問題