0

I need help with comparing two dates in php. The first date I am comparing is today's date and the other comes from the database. I tried using strtotime but it only works when the database date is less than today's date, if the date is greater than today's date I have to compare the whole date, and and strtotime does not work in that case.

Eg :

<?php
   $today=date('d/m/y');
   $expiry_date=$data['expiry'];
   if(strtotime($expiry_date)<strtotime($today)){ 
      echo "today is greater"; 
   } else {
       echo "expiry is greater";
       // only gives the right result when database result is smaller than today
   } 
?>

other method :

 $today=date('d/m/y');
   $expiry_date=$data['expiry'];
     if($expiry_date<$today)
       { echo "today is greater"; }
     else
       {echo "expiry is greater";} ?>`  // only gives the right result when database result is greater than today
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
Rebecca
  • 403
  • 2
  • 7
  • 29

2 Answers2

0

Make sure your today date format and database date format will be same...

$today=date('dd/mm/yyyy'); // DD/MM/YYYY (you are using d/m/y here )
$expiry_date="05/10/2014"; // $data['expiry']; DD/MM/YYYY Make sure for this format also

        if(strtotime($expiry_date)<strtotime($today)){
            echo "today is greater";
        } else {
            echo "expiry is greater";
            // only gives the right result when database result is smaller than today
        }
coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
0

The first method should be the correct one:

<?php
   $today=date('d/m/y');
   $expiry_date=$data['expiry'];
   if(strtotime($expiry_date)<strtotime($today)){ 
      echo "today is greater"; 
   } else {
       echo "expiry is greater";
       // only gives the right result when database result is smaller than today
   } 
?>

The second one is comparing two strings, but this is a numerical problem. The strtotime function converts the strings to numeric times that can be compared. This SO question shows a lot of examples. Compare given date with today

If you take each of the variables and do a var_dump, you can see what is being compared. Here is an example:

I manually set $expiry_date, since we don't know what format it is being retrieved in.

   $data['expiry'] = '11/12/10';
   var_dump($today, $expiry_date);
   var_dump(strtotime($today), strtotime($expiry_date));
   var_dump($today, strtotime($expiry_date));

Results:

string(8) "08/09/14" string(8) "11/12/10" 
int(1407567600) int(1289548800) 
string(8) "08/09/14" int(1289548800)

I think the most important part here is to figure out what is being retrieved from the database and in what format. First var_dump the variable $expiry_date without modifying it to strtotime. If the format is off even by just dashes or dots instead of slashes, you will get different results. This is show from the strtotime manual by sam at frontiermedia dot net dot au

<?php 
echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 

echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 

echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  
?> 

Then convert it as necessary to resemble the date variable for today ('d/m/y'), then convert both to strtotime and compare.

Community
  • 1
  • 1
nwolybug
  • 462
  • 5
  • 12