0

I'm a little new at PHP, so my script writing is still progressing. I have a page that users register for a seminar they have to pay for. If they register prior to 5 days before the seminar takes place, they get a discounted price, if they register post 5 days before the seminar takes place, they have to pay full price. I have written a PHP script that I think will do that (works is testing anyway) but I have a feeling there is a better way of writing it. I am just looking for any suggestions anyone has, if any. Your help is greatly appreciated. My script is as follows.

date_default_timezone_set('MST');
$discount_check  = date("YmdHis", mktime(date("H"), date("i"), date("s"), date("m") , date("d")+5 , date("Y") ));

//Made an array because there are several seminars a year and because I need to make it easy enough for others in my office (who don't know PHP) to add/edit seminar dates
$array = array(
'Utah_seminar' => '20120130153804',
'Seattle_seminar' => '20120723000000',
'Florida_seminar' => '20121005000000'
);

while ($seminar_dates = current($array)) 
{
    if ($discount_check >= $seminar_dates) 
    {
        echo 'discount is not eligible';
        break;
    }
    else
    {
        echo 'discount received';
        break;
    }
next($array);
}

Again, any help is greatly appreciated. Even if it's finding any hiccups with the code.

Jamie Fritz
  • 67
  • 1
  • 1
  • 8

3 Answers3

2
<?
date_default_timezone_set('MST');

$now = new DateTime();

// Made an array because there are several seminars a year and because I need
// to make it easy enough for others in my office (who don't know PHP) to
// add/edit seminar dates
$array = array(
    'Utah_seminar'    => new DateTime('2012-01-30 15:38:04'),
    'Seattle_seminar' => new DateTime('2012-07-23 00:00:00'),
    'Florida_seminar' => new DateTime('2012-10-05 00:00:00')
);

foreach ($array as $seminar => $date) {
    $cutoff = clone $date;
    $cutoff->modify('-5 days');

    if ($now > $cutoff) {
        echo "$seminar - discount is not eligible\n";
    } else {
        echo "$seminar - discount received\n";
    }
}
?>
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • Might also be an idea to put the number of days in a variable in case the specific deal gets changed? – Hecksa Jan 26 '12 at 14:18
  • Thank you so much for your answer. I'm not sure if I understand all of it, but I am searching now to see if I can't understand it. Regardless, I really appreciate it. – Jamie Fritz Jan 26 '12 at 21:10
0

Because you're using break your loop will only run once. If I understand what you're trying to do, you should use:

$eligable = false;
foreach ($array as $seminar_date) {
  if ($discount_check < $seminar_date) {
    $eligable = true;
    break;
  }
}

if ($eligable) {
  echo 'discount received';
} else {
  echo 'discount is not eligible';
}

I changed the while loop to foreach as I think they're more trivial to understand

dorsh
  • 23,750
  • 2
  • 27
  • 29
  • Thank you so much for your answer. I should have been a little more clear on my question and that was that I will probably delete a date as it passes from the array. I think that's just the nature of the beast. I really appreciate your answer and taking your time. – Jamie Fritz Jan 26 '12 at 21:12
0

Depending on what you're looking to do with the array I'd recommend sorting and then determining the first applicable index.

$discount_check = mktime(date());
//Sort array in reverse order
arsort($array);
for($i = 0; $i < count($array);$i++){
    //If array value meets the discount criteria, 
    if($array[$i]  <=   $discount_check - 432000){
    //The index of the first array item to match the discount criteria, since array is sorted all subsquent are applicable
        $split = $i;
        $i = count($i);
    }   
}
CBusBus
  • 2,321
  • 1
  • 18
  • 26