In Laravel, Carbon is a popular date/time library that provides an easy way to work with dates in PHP. To get yesterday’s date using Carbon in Laravel, you can use the subDay() method.
Here’s an example code snippet:
use Carbon\Carbon;
$today = Carbon::now();
$yesterday = $today->subDay();
echo $yesterday->toDateString(); // Output: 2023-02-24
In this example, we’re using the now() method to create a new Carbon instance representing the current date and time.
The subDay() method is then called on the $today Carbon instance, which subtracts one day from the date and returns a new Carbon instance representing the resulting date.
Finally, the toDateString() method is called on the resulting Carbon instance to output yesterday’s date in the format of “YYYY-MM-DD”.
The $yesterday variable is then output using the echo statement, which will output yesterday’s date, in this case “2023-02-24”.