Jump to content

Need help with php mysql date related query...


jpipes

Recommended Posts

Hello, I'm trying to put together a relatively simply query but I'm having trouble finding a solution that works.

 

I have a table called Calendar with 5 fields in it - ID, Month, Date, Year, and Event.

 

I'd like to run a script that will query this table and based on the current actual date, will pull out those entries that match the same Month and Date entry in the table. If today is January 13 the result will display all entries under Month = January and Date = 13 from within the table. 

 

Does anyone have a suggestion or a possible bit of code I could use to accomplish this?

 

Thanks in advance to anyone with any help! 

Link to comment
Share on other sites

<?php

$day = date("j"); // 1-31
$month = date("n"); // 1-12
$year = date("Y");

$sql = "SELECT * FROM `calendar` WHERE `day`='".$day."' AND `month`='".$month."' AND `year`='".$year."'";
$res = mysql_query($sql) or die(mysql_error());

if(mysql_num_rows($res) == 0){
echo "Today is a boring day, nothing is happening!";
}else {
echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
while($row = mysql_fetch_assoc($res)){
	echo "<tr><td>".$row['event']."</td></tr>\n";
}
echo "</table>\n";
}

?>

Link to comment
Share on other sites

I tried it and didn't get any errors but I also didn't get any data either. My database has the month stored as "January" or "March" versus 1-12, if that matters. Again, the fields in the table called Calendar are ID, Month, Date, Year, and Event.

 

Some more background, I'm trying to create a "on this day in history" sort of script where based on todays date (January 13 for example) anything in the database that occured on January 13 will appear.

Link to comment
Share on other sites

I tried it and didn't get any errors but I also didn't get any data either. My database has the month stored as "January" or "March" versus 1-12, if that matters. Again, the fields in the table called Calendar are ID, Month, Date, Year, and Event.

 

It sure does.  Modify that script to use the same names as your database table uses.  Modify the date determination lines to get the day, month, year in the same format as your database table uses.  Refer to the date() function in the manual to see just how to do that.

 

http://ca.php.net/manual/en/function.date.php

Link to comment
Share on other sites

That did it! Thanks. I changed date("n") to date("F") and it works! ;D Thank you!!

 

Now I just need customize the output and whatnot. I spent a better part of the afternoon working on this and got a working answer in 15 min or so after posting here. I'm stunned. Thank you.

 

Side question - is it possible to run a php script like this on a page not called xyz.php?

 

 

Link to comment
Share on other sites

That I know, but I am interested in parsing the above php script into an html document. I found out that by adding

 

RemoveHandler .html .htm

AddType application/x-httpd-php .php .htm .html

 

to the .htaccess file, php scripts will work ok on html pages without having to rename or call them xyz.php! Very cool!

Link to comment
Share on other sites

Just a quick update... the script is working great but after I added the above info to my .htaccess file although php worked embedded in html pages, some of my other scripts (.pl files specifically) stopped working...

 

Anyone know of a "safe" way to parse or embed php into an .html document?

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.