I'm building a website for a school, with a place the teachers can add assignments. All these assignments are stored in a MySQL table, with a field of type datetime that stores the due date. I'm looking for a way, with PHP, to check if the due date is in the past so I can provide visual feedback for the teachers and students. Any help would be appreciated!
-
3There are several different ways of doing this. What have you tried? – John Conde Mar 13 '13 at 18:32
3 Answers
I would use PHP's date comparison tools. Here is an example:
$date1 = new DateTime("now");
$date2 = $yourDateFromTheDatabase;
if ($date2 < $date1) {
//it still has time
} else {
//time has passed
}
Does that help? You can find more examples here: http://php.net/manual/en/datetime.diff.php

- 324
- 2
- 13
You could actually handle this with MYSQL in your query, which i would find easier because then you don't have to handle dates in php. For example:
SELECT DueDate, (DueDate < CURDATE()) FROM table;
(DueDate < CURDATE()) returns 0/1
otherwise:

- 1
- 1

- 575
- 3
- 9
Depending on how you're storing dates into the datetime field (hopefully you're using TIMESTAMP), you can use the PHP strtotime function, which parses the MySQL timestamp into a Unix timestamp which can be used for comparing PHP values. Here's an example:
$res = mysql_query("SELECT datetime FROM assignments;");
while ( $row = mysql_fetch_array($res) ) {
$date = date("g:i a F j, Y ", strtotime($row["date"]));
}
And do your comparisons with the $date
variable. That's a super broad and rough look at an idea, but hopefully it helps somewhat.
You can read more about this concept here.
Good luck!

- 4,302
- 2
- 25
- 40