Jump to content

Recommended Posts

 

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?

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/
Share on other sites

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>";
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030123
Share on other sites

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!

 

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030259
Share on other sites

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 :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030262
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030263
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030264
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/196161-ifthenand-dates/#findComment-1030276
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.