Ipssissimus Posted January 22, 2012 Share Posted January 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/ Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2012 Share Posted January 22, 2012 The values you're supplying to strtotime aren't valid date formats, so strtotime() is returning FALSE. There's a link to the accepted formats in the manual entry. Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310040 Share on other sites More sharing options...
AyKay47 Posted January 22, 2012 Share Posted January 22, 2012 strtotime() is expecting a year as well, so "23-12-2011" Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310043 Share on other sites More sharing options...
Ipssissimus Posted January 22, 2012 Author Share Posted January 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310051 Share on other sites More sharing options...
AyKay47 Posted January 22, 2012 Share Posted January 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310053 Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2012 Share Posted January 22, 2012 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' Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310055 Share on other sites More sharing options...
Ipssissimus Posted January 22, 2012 Author Share Posted January 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310058 Share on other sites More sharing options...
Ipssissimus Posted January 22, 2012 Author Share Posted January 22, 2012 Thanks for your help. A little trial and error and I've resolved it. I didn't need to to have (strtotime($today) within the script only for the hard coded dates. Regards Andrew Quote Link to comment https://forums.phpfreaks.com/topic/255519-compare-current-date-within-a-range-of-24-dates-using-if-else/#findComment-1310068 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.