php:测试数组行,日期的条件范围是在日期范围之间

时间:2013-09-24 15:59:53

标签: php arrays date if-statement

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

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

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

2 个答案:

答案 0 :(得分: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++;
}

答案 1 :(得分: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';
                    }
            }