PHP : 测试数组行 日期的条件范围介于日期范围之间


php : test for lines of array the condition range of date is between range of date

我在如何过滤日期范围内的条目方面存在逻辑问题。

数组$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

从 648-08-2014 开始是 05 欧元,然后从 750-2014-08-05 变成 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++;
          }

我尝试了很多事情都没有成功。

我喜欢使用日期时间将日期字符串转换为日期对象。否则,您的>运算符正在处理两个字符串,并且字符串的比较方式与日期不同。可能有更好的方法...

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

我终于设法做到了:

- 将日期转换为日期时间对象
- 在解析数组时创建 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';
                    }
            }