Reputation: 1276
I'm just being confused right now about how to make the comparing of date using php works perfectly. I have compared the current date to the expiry date and highlight those dates that are lesser than the current date with red color. However, it shows that July 2, 2016 is greater than August 22, 2016 which is wrong. It doesn't work also by comparing it on december. What is the problem with this? Please help. THanks a lot. Here is my code.
$currDate = date('F j, Y'); //July 2, 2016
<?php
if($currDate > date("F j, Y", strtotime($val->purchases->expiry_date_month))){ ?>
<td style="color:#fff; text-align:center;background-color:#C00;">{{ date("F j, Y", strtotime($val->purchases->expiry_date_month)) }}</td>
<?php }
Output:
Upvotes: 0
Views: 1109
Reputation: 351403
The problem is that you are comparing strings, and that the string representation you use does not sort in chronological order: "July" > "August".
Use date()
when you want to format dates for output. For comparing dates, it is less useful, although possible if you use a format for which the alphabetical order corresponds to the date order (e.g. "Y-m-d"). But you are then still comparing strings. It is more efficient to compare timestamps, which are numerical:
$currDate = time();
$expiry = strtotime($val->purchases->expiry_date_month);
if($currDate > $expiry) { ?>
<td style="color:#fff; text-align:center;background-color:#C00;">
{{ date("F j, Y", $expiry) }}
</td>
<?php }
Upvotes: 2
Reputation: 1276
I just change this line $currDate = date('F j, Y');
to $currDate = date('Y-m-d');
Upvotes: 0