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-08-05 开始是 648 欧元,然后从 2014-08-05 变成 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将日期字符串转换为日期对象。否则,您的>操作员正在处理两个字符串,并且字符串的比较与日期不同。可能有更好的方法...

$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 回答
0

我终于设法做到了:

- 将日期转换为 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';
                    }
            }
于 2013-09-25T13:31:10.000 回答