2011-06-09 86 views
0

我要去實現MySQL中的搜索功能的應用程序林建設, 用戶鍵入[現在,以後將是一個日曆選取]開始日期和結束日期, 並點擊搜索驗證日期範圍搜索

  1. 如果(開始日期>結束日期),然後按錯誤
  2. 如果(開始一個月>月末),則差錯

好了,到目前爲止,但有考慮時

  1. 如果(開始日期>結束日)& &(起始月<末月),然後搜索
  2. 如果(起始月份>月末)& &(開始年度<末年),然後搜索

我不擔心閏年,我會循環所有的月份到31天,因爲如果一天是在數據庫中,它將獲取它,其他明智的,它會去31,並沒有返回,因爲沒有一天,

Im使用VARCHAR我的時間(沒有時間戳),因爲它們是從JSON進口[的iOS]

好了,希望是有道理的,

這裏的代碼,

<?PHP 
    $start = $_POST['start_date']; 
    $end = $_POST['end_date']; 
    $start_time = explode('/', $start); 
    $end_time = explode('/', $end); 
$count_start = $start_time[0]; 
    $count_end = $end_time[0]; 
    $month_start = $start_time[1]; 
    $month_end = $end_time[1]; 
    $year_start = $start_time[2]; 
    $year_end = $end_time[2]; 

    function cuenta($count_start, $count_end) { 

for($count_start; $count_start <= $count_end; $count_start++) { 

    print $count_start . "<BR>"; 
} 

    } 

    if (!isset($_POST['Submit1']) || ($start == "Start Date" && $end == "End Date") || ($start == "" && $end == "End Date") || ($start == "Start Date" && $end == "") 
|| ($start == "" && $end == "")) 

    { 

print ("no data yet")."<BR>"; 
    } 

    if ($year_start[2] > $year_end[2]){ 

print ("Please make sure end date Year is equal or greater than start date Year"); 
} 

    if (($month_start > $month_end) && ($year_start <= $year_end)){ 

print ("Please make sure end date Month is greater than start date Month"); 

    } 
    elseif (($month_start > $month_end) && ($year_start < $year_end)){ 

cuenta($count_start, $count_end);   
    } 

    elseif ($count_start > $count_end) { 
print ("Please make sure end date Day is greater than start date Day"); 

    } 


    ?> 

即時通訊在beginining在PHP中,對不起,如果我想念明顯![這就是爲什麼即時通訊要求哈哈] 因此,如果我缺少一些其他重要的驗證日期範圍之間搜索,請讓我知道瓦特,也plz指向我在這個ifs森林的最佳方向!

非常感謝!

+0

好的,你得到一個錯誤? – Ibu 2011-06-09 05:36:49

+0

嗨,tnx,沒有錯誤,只是沒有按預期工作 – MaKo 2011-06-09 05:42:29

回答

2

它更容易將日期轉換爲unix時間戳,然後比較整數。 PHP爲此具有函數strtotime。如果你的日期已經分割,你可以使用mktime。

+0

嗨,非常感謝!,非常優雅的功能strtotime !!,太棒了! – MaKo 2011-06-09 06:56:31