2013-09-24 31 views
0

我對如何在日期範圍內篩選條目存在邏輯問題。php:檢測數組的行數日期的條件範圍在日期範圍之間

數組$price包含價格取決於旅行期間的飛行,按日期排序:從2014-08-

print_r($price) 
    [21] => Array    
     [date_beg] => 2014-07-05    
     [total1] => 648 

    [22] => Array 
     [date_beg] => 2014-08-05    
     [total1] => 750 
    [23] => Array 
     [date_beg] => 2014-08-12    
     [total1] => 520 

它是從2014年8月5日648歐元,然後就變成750 05 ...

我在某些時段還有特別優惠。

相同的信息是存在用於該陣列的每個元素:

print_r($price) 
    [21] => Array    
     [date_beg] => 2014-07-05    
     [total1] => 648 

     [special_beg] => 2014-07-07 
     [special_end] => 2014-08-06 
     [special_price] => 600 

    [22] => Array 
     [date_beg] => 2014-08-05    
     [total1] => 750 

     [special_beg] => 2013-10-27 
     [special_end] => 2013-12-18 
     [special_price] => 600 

    [23] => Array 
     [date_beg] => 2014-08-12    
     [total1] => 520 

     [special_beg] => 2013-10-27 
     [special_end] => 2013-12-18 
     [special_price] => 600 

我想要做的是將含有特殊價格的細節爲陣列的每個元件的新的「顯示」陣列該促銷有效。

喜歡的東西:

  $nb_date=0; 
      foreach($price as $a_price){ 
       if (($a_price['special_beg']>$a_price['date_beg'])) 
     //+ some other conditions 
     { 

        $display[$nb_date]'special_beg']=$a_price['special_beg']; 
        $display[$nb_date]'special_end']=$a_price['special_end']; 
        $display[$nb_date]'special_price']=$a_price['special_price']; 
        $display[$nb_date]'promo_web']=$a_price['promo_web']; 
        $display[$nb_date]['promo_text']=$a_price['promo_text']; 
        ...etc... 
       } 
      $nb_date++; 
      } 

我曾嘗試沒有成功不少東西。

+0

請發表您已經嘗試過。 – Pietu1998

回答

1

我喜歡用DateTime將日期字符串轉換爲日期對象。否則,您的>運算符正在處理兩個字符串,並且字符串得到的比較與日期不同。有可能是一個更好的辦法...

$nb_date=0; 

//Make sure to set Default Timezone when working with DateTime class 
//Set This from the list: http://www.php.net/manual/en/timezones.php 
date_default_timezone_set('America/New_York'); 

//DateTime object with current Date/Time 
$now = new DateTime(); 

foreach($price as $a_price){ 

    //I *think* this is the logic you want, making sure the current 
    //date is after the beginning date. 

    $date_beg = new DateTime($a_price['date_beg']); 
    if ($now > date_beg){ 

     $display[$nb_date]['special_beg']=$a_price['special_beg']; 
     ... 

    } 
    $nb_date++; 
} 
+0

謝謝,但它不會過濾出special_end> date_end – Matoeil

+2

您需要將該條件添加到'if'語句。我不知道你在尋找什麼特定的條件。你的代碼顯示了「+其他一些條件」 - 這就是應該去那裏。使用我提供的代碼爲您正在談論的日期創建DateTime對象,並將它們放入'if'語句中。我試圖幫助您編寫代碼,而不是爲您編寫代碼。 – Dutchie432

0

我終於成功地做到這一點是這樣的:

-convert日期DateTime對象
- 創建2個條件,在分析陣列和交換機說我們現在正在進入促銷日期...類似的東西:

$promo_has_started='false'; 

      foreach ($grille_tarifs as $key => $value) 
      { 

      $date_promo_resa_debut = new DateTime($grille_tarifs[$key]['date_promo_resa_debut']); 
      $date_promo_resa_fin = new DateTime($grille_tarifs[$key]['date_promo_resa_fin']); 
      $date = new DateTime($grille_tarifs[$key]['date']); 
      $date_fin= new DateTime($grille_tarifs[$key]['date_fin']); 


       if (($grille_tarifs[$key]['promo_heberg']==$_POST['vt_heberg'])||($grille_tarifs[$key]['promo_heberg']==$_POST['vt_croisi'])||(!(isset($_POST['vt_heberg'])))&& (!(isset($_POST['vt_croisi'])))) 


        if (($date_promo_resa_debut<$date_fin)&&($promo_has_started=='false')) 
        { 
         $promo_has_started='true'; 
         $grille_tarifs[$key]['promo_web']='display'; 

        } 
        else if (($promo_has_started=='true')&&($date_promo_resa_fin >=$date)) 
        { 
         $promo_has_started='true'; 
         $grille_tarifs[$key]['promo_web']='display'; 
        } 
        else 
        { 
         $grille_tarifs[$key]['promo_web']='oui'; 
        } 
      } 
+0

太棒了!您應該刪除此答案,並將其內容附加到原始問題。 – Dutchie432