n2xlr8n Posted March 22, 2010 Share Posted March 22, 2010 My church Web site uses PHP, which I am not familiar with. I only have access to the body of any page, not the head. I want to create a list of future events, and I want them to appear and disappear on certain dates. In other words, I don't want to have to go in and add new HTML code on the day I want the code to appear, then go in and delete the HTML code when the event is over. I want to prepare the list once for the entire month, then forget about it. So I'm looking for a basic line or two of code that says this: If today's date is greater than or equal to the display date, AND today's date is less than or equal to the event date, display my message. To start with, I've tried this code and the message appears, but it doesn't disappear on the date: <? $d=date("m/d/Y"); if ($d<="3/21/2010") { echo "<p>This message should appear"; echo "only if today's date"; echo "is less than or equal to 3/21/2010.</p>"; } ?> What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/ Share on other sites More sharing options...
inversesoft123 Posted March 22, 2010 Share Posted March 22, 2010 Is there any problem you are not using timestamp ? <? $d=time(); if ($d<="1269285651") // 10 mins back { echo "<p>This message should appear"; echo "only if today's date"; echo "is less than or equal to 3/21/2010.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030123 Share on other sites More sharing options...
n2xlr8n Posted March 22, 2010 Author Share Posted March 22, 2010 Thanks for the response. Yes, I am not using timestamp because 1) I don't know any PHP, and so I don't know what timestamp means in this case, and 2) I want to deal with dates, not time. For example, today is 3/22/2010. I want to create a message today that will automatically appear on the Web page on 4/1/2010 (and not before), and I want it to disappear on 5/1/2010. There are no times involved, just dates. If timestamp is a function that will do the job, I'll use it. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030259 Share on other sites More sharing options...
Jax2 Posted March 22, 2010 Share Posted March 22, 2010 <? $d=date("m/d/Y"); if (($d<="3/21/2010")&&($d !>"3/21/2010")) { echo "<p>This message should appear"; echo "only if today's date"; echo "is less than or equal to 3/21/2010.</p>"; } ?> Try that? Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030261 Share on other sites More sharing options...
Jax2 Posted March 22, 2010 Share Posted March 22, 2010 One other thing you could do is set up a MySql table for the messages ... simply make it look like this: ID (unique) date (date) message (text) and then you can have a single code: $today=date("m,d,Y"); $sql="SELECT * FROM table WHERE date=$today"; $result=mysql_query($sql, $db); while $row=(mysql_fetch_array($result)) { echo $row['message']; } and then simply start adding messages for the days you want them to be displayed on. Nice and easy Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030262 Share on other sites More sharing options...
n2xlr8n Posted March 22, 2010 Author Share Posted March 22, 2010 Thanks, but I can't set anything up. This Web site is provided by the world church, and the local church members have very limited access. All we can do is some HTML (and hopefully PHP) code and insert it into a pre-designed page. I'll try your 7:22:33 post example and see if I can get that to work. Thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030263 Share on other sites More sharing options...
PFMaBiSmAd Posted March 22, 2010 Share Posted March 22, 2010 A little number/string comparison theory - you can only compare dates when the format is left-to-right, most significant digit (year) to least significant digit (day), and the length of each corresponding field is the same (you need leading zero's) which is one reason why databases use a YYYY-MM-DD format. 'Y-m-d' would be the recommend date() format string to use. Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030264 Share on other sites More sharing options...
n2xlr8n Posted March 23, 2010 Author Share Posted March 23, 2010 I modified Jax2's example and it worked. I then applied PFMaBiSmAd's date() format string suggestion for good measure. <? $d=date("Y-m-d"); if (($d>="2010-03-21")&&($d<"2010-04-03")) { echo "Success speaks for itself!"; } ?> Life is great! Thanks much!! Quote Link to comment https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030276 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.