Jump to content

Recommended Posts

I'm trying to select records from the database that fall between the start date and the end date AND also (if they are recurring events) fall on the correct day of the week or the date of the month.

 

Here's what I have:

 

$newday = $_GET['day'];

$sql = "SELECT * FROM dayplanner WHERE $newday BETWEEN startdate AND enddate AND '$newday = 'date('l')' OR '$newday = 'date('d')''";

 

The format of $_GET['day'] is the mysql date format (2009-05-07).  Obviously, my code won't work, because I haven't figured out what day of the week, or date of the month, the url is passing.

 

How can I determine from 2009-05-07 what day of the week and date of the month it is?

 

Thanks.

Uh, probably using a few of the Mysql functions found here:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofmonth

 

If you have trouble, first be sure you can correctly select dates between startdate and enddate.  Then add your AND clause, and probably use parentheses to enforce the logic you are trying to achieve.

Thanks for the help...the first part of the query works.  The only part that doesn't is the "recurring" part.

 

Here's the new code:

 

$sql = "SELECT * FROM dayplanner WHERE $newday BETWEEN startdate AND enddate AND (recurring = 'DAYOFMONTH('$newday')' OR recurring = 'DAYOFWEEK('$newday')')";
          
$rs = mysql_query($sql,$dbc);  
$matches = 0; 
while ($row = mysql_fetch_assoc($rs))  {
$matches++; 
echo "$row[event]<br />";
}  
if (! $matches) { 
echo "no matches here"; }  

 

It throws an error on this line...

 

while ($row = mysql_fetch_assoc($rs))  {

 

...so it's not pulling the correct date or day from the db.

 

Could this have something to do with the fact that using BETWEEN removes the dashes from the date field?  So instead of 2009-05-07, it's 20090507.

 

To help in troubleshooting, I've added a field in the database.

 

I now have a field for dayofweek, and a field for dayofmonth (these are for the recurring events).

 

Unfortunately, the code still fails:

 

$sql = "SELECT * FROM dayplanner WHERE $newday BETWEEN startdate AND enddate AND (dayofmonth = 'DAYOFMONTH('$newday')' OR dayofweek = 'DAYOFWEEK('$newday')')";

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.