0

How can I check if a date captured in the database is less than the current date?

foreach( $results as $r ) {
$date = $r['date'];

if($date < $currentdate) {
echo 'oldest dates';
} else {
echo 'newest dates';
}
  • 3
    Compare the timestamps instead: `if (strtotime($date) < strtotime($currentdate))` or use DateTime objects instead. – Amal Murali Mar 08 '14 at 04:54

2 Answers2

1

Use strtotime function:

$date  = strtotime($date);
$currentdate = strtotime($currentdate);
Nathan
  • 1,220
  • 3
  • 15
  • 26
Mayur
  • 227
  • 1
  • 8
0

Use strtotime($date)<strtotime($currentdate)
and make sure that both dates are in correct format.

Nemesis
  • 121
  • 2
  • 11