Home PHP How to compare two dates using PHP?
 
 
 

How to compare two dates using PHP?

PDF Print E-mail

Question: How to compare two dates using PHP?

 

Answer:

  • First we need to convert the two dates to UNIX Timestamps using the PHP function strtotime
  • Then we can compare the two dates in anyway we wish, in the example below I'm checking that the first date is older than the second date
     
    $date1 = '1999-10-11';
    $date2 = '2010-10-11';
     
    $firstDate = strtotime($date1);
    $secondDate = strtotime($date2);
     
    if($firstDate < $secondDate){
         echo 'First date is older';
    }else{
         echo 'Second date is older';
    }
     
     
+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!