Jump to content

mysql query with two date columns


sw45acp

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");

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.