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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.