6

How can I check if the date is later than the current date?

if(date("d") < "18" && date("m") < "01") { 
    echo 'To late!';
}

Doesn't work for me.

Barmar
  • 741,623
  • 53
  • 500
  • 612
zerophreak
  • 294
  • 1
  • 4
  • 11
  • 1
    If you want to check if the current date is after something, why are you using less-than? – Barmar Jan 28 '14 at 17:30

3 Answers3

7

You can do this notation:

if( '20140505' > date("Ymd") ) {
    // today's date is before 20140505 (May 5, 2014)
}
Patrick Moore
  • 13,251
  • 5
  • 38
  • 63
Niels
  • 48,601
  • 4
  • 62
  • 81
  • Which requires you to manually construct the date string. I don't think that's a very good idea. Use `strtotime()` - or *even* better, use `DateTime` class. – Amal Murali Jan 28 '14 at 17:37
5
$time1  = strtotime(date("d/m/Y", "18/01/2014"));
  if(time() > $time1)
   {
     echo "too late!";
   } 
Zakaria.dem
  • 293
  • 5
  • 10
0

The best is to use strtotime.

$current_date = date("Y-m-d");
$date_to_compare = date("Y-m-d",time()+86400); //1 day later
if (strtotime($date_to_compare) > strtotime($current_date)) {
   echo "too late";
}
bug
  • 342
  • 1
  • 10