Reputation: 7289
I have a logic problem on how to filter entries within a range of date.
An array $price
contains prices for a flight depending of the period of travel, ordered by date:
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
It is 648 euros from 2014-08-05, then it becomes 750 from 2014-08-05 ...
I also have special offers at some periods.
The same information is present for each element of the array:
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
What I would like to do is feeding a new "display" array containing details of the special price for each element of the array that the promotion is valid for.
Something like:
$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++;
}
I have tried quite a few things with no success.
Upvotes: 0
Views: 246
Reputation: 7289
i have finally managed to do it like this :
-convert date to Datetime object
-create 2 conditions while parsing the array and a switch to say we are now entering promotional dates...something like that:
$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';
}
}
Upvotes: 0
Reputation: 29160
I like to use DateTime to convert a date string to a date object. Otherwise your >
operator is working on two strings and strings get compared differently than dates do. There may be a better way...
$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++;
}
Upvotes: 1