Jump to content

How to test several conditions?


parboy

Recommended Posts

I am trying to select text to display on condition of the current date.

I've set a Unix timestamp for the current date:

<?php
$todays_date = date("Y-m-d"); 
$today = strtotime($todays_date); 
?>

Next I test for the first condition. I want the text to appear for 2 weeks then terminate:

<?php
$exp_date = "2011-05-09"; 
$expiration_date = strtotime($exp_date); 
$start_date = strtotime("-14 days", $expiration_date);
if ($start_date < $today && $expiration_date > $today) { 
echo "<h2>Special Event #1</h2>";
} elseif {

 

At this point, I want to set the exp_date for the next event and test again, but I don't know how to do it. The techniques I've tried appear to evaluate as false and display the default text which follows the final "else":

} else {
echo "<h2>Default Text</h2>";
}
?>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/212731-how-to-test-several-conditions/
Share on other sites

That's pretty much the code I've written. The conditional statements are all based on the event's expiration date which is defined:

	$exp_date = "2011-05-09"; 

The special text is also defined for each event:

	echo "<h2>Special Event #1</h2>";

I had intended to repeat the conditional test for each event with a string of "elseif" conditional statements, but it doesn't work. I think one problem is how to reset the exp_date within a conditional statement.

The question of how to reset $exp_date is closely tied into where $exp_date comes from.  Do you have an array of events?  Are you getting them from the database?    How about something like this:

 

$events = array(
  array(
    'exp_date' => '2010-08-01',
    'start_date' => '2010-07-01',
    'name' => 'July Madness',
  ),
  array(
    'exp_date' => '2010-09-01',
    'start_date' => '2010-08-01',
    'name' => 'August Apathy',
  ),
);

foreach ($events as $event) {
  $event_exp_date = $event['exp_date'];
  $event_start_date = $event['start_date'];
  $event_name = $event['name'];

  if (...) {
  } elseif ( ...) {
  }
}

 

Then all your conditions can go in there.  The event variables will be set to the ones for each event in turn.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.