0

日付の範囲内でエントリをフィルタリングする方法について論理的な問題があります。

配列$priceには、日付順に並べられた、旅行期間に応じたフライトの価格が含まれています。

   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 ユーロですが、2014 年 8 月 5 日から 750 ユーロになります ...

時期によってはお得なキャンペーンもやっています。

配列の各要素に対して同じ情報が存在します。

 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++;
          }

私はかなりの数のことを試しましたが、成功しませんでした。

4

2 に答える 2

1

私はDateTimeを使用して、日付文字列を日付オブジェクトに変換するのが好きです。そうしないと、>オペレーターは 2 つの文字列を処理し、文字列は日付とは異なる方法で比較されます。もっと良い方法があるかもしれません...

$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++;
}
于 2013-09-24T16:42:32.837 に答える