PASSWORD RESET

Your destination for complete Tech news

How to get yesterday’s date in Laravel Carbon?

3.44K 0
< 1 min read

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”.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.