Comparing two dates in blade.php file

✔ Recommended Answer

@php    use Carbon\Carbon;    $today_date = Carbon::now();    foreach ($entries as $k => $entry) {        $expire_date = Carbon::createFromFormat('Y-m-d', $entry->expire_date);        $data_difference = $today_date->diffInDays($expire_date, false);  //false param        if($data_difference > 0) {            //not expired        }        elseif($data_difference < 0) {            //expired        } else {            //today        }    }@endphp

The false parameter is optional and indicates if you want the return value to be the absolute value or a relative value that might have a - (negative) sign if the passed in date is less than the current instance. This will default to true, return the absolute value.

More here

Source: stackoverflow.com

Answered By: Sreejith BS

Method #2

To compare two dates in a Blade template file, you can use PHP's built-in strtotime() function to convert the dates to Unix timestamps, which are integer values representing the number of seconds since January 1, 1970. You can then compare the two timestamps using standard comparison operators.

Here's an example code snippet that demonstrates how to do this:

less
@if(strtotime($date1) > strtotime($date2)) Date 1 is later than date 2. @else Date 2 is later than or equal to date 1. @endif

In this example, $date1 and $date2 are variables that contain the dates you want to compare. The strtotime() function converts each date string to a Unix timestamp, which can then be compared using the > operator.

Note that the strtotime() function may not work correctly with all date formats, so you may need to adjust the format of your date strings to ensure that they can be correctly parsed.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755