In this article, we will explore how to check if a date is in the past using Laravel Carbon. We’ll provide a simple example of using Carbon's isPast() method to determine whether a given date has already passed. This method works in various Laravel versions, including Laravel 10, and 11.
By the end of this guide, you’ll know how to effectively use Carbon's isPast() function to compare dates and handle time-based logic in your Laravel applications.
How to Check Date is Past in Laravel 11 Carbon:
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$myDate = '12/08/2024';
$result = Carbon::createFromFormat('m/d/Y', $myDate)->isPast();
var_dump($date);
}
}
use Carbon\Carbon;
// Example Date
$date = '2023-12-01';
// Check if Date is in the Past
if (Carbon::parse($date)->isPast()) {
echo "The date $date is in the past.";
} else {
echo "The date $date is not in the past.";
}
// Example Date
$date = '2023-12-01';
$currentDate = date('Y-m-d');
// Check if Date is in the Past
if (strtotime($date) < strtotime($currentDate)) {
echo "The date $date is in the past.";
} else {
echo "The date $date is not in the past.";
}
You might also like: