Jump to content

Recommended Posts

I have a table that stores college assignments. In the table there are three columns: assignments, date, and due date, where the "date" is the date that it was assigned or handed out, and the "due date" is the date that the assignment is due. Both the "date" and "due date" columns are formatted with the mysql date format. What I would like to do is retrieve all assignments where today's date is between those two date columns.

 

This doesn't work:

$date = date("Y-m-d");
$q = mysql_query("SELECT * FROM `table_college` WHERE `date` >= '$date' AND `due_date` <= '$date'");

 

Any help would be appreciated, thank you.

Link to comment
https://forums.phpfreaks.com/topic/195803-mysql-query-with-two-date-columns/
Share on other sites

You need to use time() instead of date.

 

It will assign a time when you create the record, and then you can use time() again for the "due date"...

 

Make sense?

 

I.e., if you want it due 24 hours past when you assigned it, it would be

 

start time + 86000 (24 hours).

 

Then when you do a search, it's as simple as

 

Find all records where time() > start time and time() < end time ...

 

time() spits out something like "1269007275". Should I format the date columns to be "TIME" instead? When I insert something from time() it says data type fractured (in phpMyAdmin). So should I format the "date" and "due_date" columns to simply be an integer with 10 as a length?

Good God. Don't use the time() function.

 

You already have the data in the optimum format for storing, comparing, and retrieving dates. Just fix the logic in the query -

 

$q = mysql_query("SELECT * FROM table_college WHERE '$date' BETWEEN date AND due_date");

 

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.