Jump to content

Compare current date within a range of 24 dates using if else


Ipssissimus

Recommended Posts

I'm trying to do the following PHP. I have written it in English

 

if (today's date) => date1 AND <= date2 then {display image1}

elseif (today's date) => date3 AND <= date4 then {display image2}

else {display image 13}

 

There are 24 fixed dates and 13 fixed images.

 

I have tried using combinations of strtotime(), replacing the date value with variables, hard coding the dates within the program. I don't seem to be able to get any combination to work properly. I'm sure it's just a syntax error but I can't see it. When I've searched the web all the answers I've found relate to dates within databases but as I only have 24 dates it seems a bit of overkill. I would appreciate any pointers to the correct method I might be able to use. The dates need only a day and month as I would like this to repeat year after year.

 

<?php

$today = strtotime(date('d-m'));

 

if (strtotime($today) >= strtotime('28-10') && strtotime($today) <= strtotime('24-11')) {echo "<div>image1</div>";}

elseif (strtotime($today) >= strtotime('25-11') && strtotime($today) <= strtotime('22-12')) {echo "<div>image2</div>" ;}

elseif (strtotime($today) >= strtotime('23-12') && strtotime($today) <= strtotime('24-02')) {echo "<div>image3</div>" ;}

else {echo "<div>image13</div>";}

?>

 

The result I get from this is image1 appears on the web page but I would expect image3 as today is 22-01

 

Thank you in advance.

 

Andrew

Link to comment
Share on other sites

Hi folks

 

Thak you for your pointers.

 

I have updated the variable to $today = strtotime(date('dd-mm-YY')); and added a 4 digit year to the dates within the script. I now get the "else image13". Can you offer anymore pointers please as to where I'm going wrong.

 

Many thanks (getting balder by the minute)

 

Andrew

Link to comment
Share on other sites

Hi folks

 

Thak you for your pointers.

 

I have updated the variable to $today = strtotime(date('dd-mm-YY')); and added a 4 digit year to the dates within the script. I now get the "else image13". Can you offer anymore pointers please as to where I'm going wrong.

 

Many thanks (getting balder by the minute)

 

Andrew

can you post the updated code.

Link to comment
Share on other sites

The date() format specifier - 'dd-mm-YY' produces 2222-1010-20122012 for today's date.

 

Since you want this to be year neutral, you can just compare the month number and day number (with leading zero's) and with the month first (most significant digits) and day second (least significant digits.) However, since one of the date ranges spans the end of one year/start of next, you would need to take into account the date rollover. The easiest way, since you are hard-coding the dates anyway, would be to break that date range up into '12-23' to '12-31' and '01-01' to '02-24'

Link to comment
Share on other sites

Here's the code I've updated

 

$today = strtotime(date('dd-mm-YY'));

 

if (strtotime($today) >= strtotime('28-10-2011') && strtotime($today) <= strtotime('24-11-2011')) {echo "<div>image1</div>";}

elseif (strtotime($today) >= strtotime('25-11-2011') && strtotime($today) <= strtotime('22-12-2011')) {echo "<div>image2</div>" ;}

elseif (strtotime($today) >= strtotime('23-12-2011') && strtotime($today) <= strtotime('24-02-2012')) {echo "<div>image3</div>" ;}

else {echo "<div>image13</div>";}

 

This shows image13 for today's date (22-01-2012)

 

Many thanks

 

Andrew

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.