-3

I've created an If an Else Statement but it doesn't work properly. I have some dates within my SQL which have been retrieved and stored in variables using PHP.

I'm comparing the current date with the dates from the database but for some reason, it thinks for example that 29-09-2015 if LESS THAN 31-01-2015.

I can understand that the format could be the issue d,m,Y but I thought I'd corrected that already. Here's the code:

$today = date('d-m-Y');
$date = $row['respondby'];
$euDate= date("d-m-Y", strtotime($date));

<?php 


if($today < $euDate){                                                           echo "<td>". $today." is less than ". $euDate ."</td>";
}
else{
echo"<td>Lost somewhere in between ?!?!?! :S </td>";
}
?>

As a result it prints 29-09-2015 is less than 30-06-2015

today's date was 29-09-2015 and one of the dates was in the data was this one as shown.

Thank you everyone that helps.

1 Answers1

1

Comparison of dates as strings uses lexicographical order, hence your result is "correct".

Instead of d-m-Y format, try to use Y-m-d, this guarantees proper ordering.

$today = date('Y-m-d');
$date = $row['respondby'];
$euDate= date("Y-m-d", strtotime($date));
if($today < $euDate) { [...] }

Or, you can use Date objects instead:

$today = new Date('now');
$euDate= new Date($row['respondby']);
if($today < $euDate) { [...] }
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
  • I believe the problems lays within the sql, because the data that is been extracted seems to be in the default. (Y- m- d) also i've learn looking around the web that there can be a big difference between using - and / to separate dates. I live in europe so the order of the format is expect as (d-m-Y) i've got it working by doing strtotime not only on the variables but also AGAIN (for some reason) inside the condition, and then finally print the result. Although I'm told this could result as a slow method so much formatting. – Joao Harry Woodford Sep 30 '15 at 13:50