How To Use Carbon In Laravel 9 Blade And Controller?

Hello Artisan,

In this example, I will show you how to use carbon in the Laravel blade file as well as a controller in the Laravel 9 application. You know that we use carbon to format date. carbon is a great php library to convert date to human readable format. there are many functions available in carbon to convert date format.

You know that sometimes we need to convert or format the date in the blade file and also in Laravel controller. So in this laravel 9 carbon tutorial, i will show you gow to use carbon in a proper way in Laravel blade and controller.

So in this laravel 9 carbon tutorial, you will learn how to use carbon in laravel blade and use carbon in laravel controller. So let see the example of carbon date formate laravel.

Just use the below code to the top of your controller before using carbon in laravel.

use Carbon\Carbon;

 

After importing, let's have a look at some cool things we can do with this great package.

get current time

$current = Carbon::now();

 

get today yesterday and tomorrow

$today = Carbon::today();
$yesterday = Carbon::yesterday();
$tomorrow = Carbon::tomorrow();

 

Read also : Database Transaction | When and How to Use Database Transactions in Laravel

 

For example, suppose when we create a trial period for a user, you will want the trial period to expire after a certain amount of time. So let's say we have a 30 day trial period. We could easily calculate that time with add and subtract.

// get the current time
$current = Carbon::now();

// add 30 days to the current time
$trialExpires = $current->addDays(30);

 

From the Carbon docs, here are some of the other add() and sub() methods available to us


$dt = Carbon::parse($data->created_at) //from database

echo $dt->toDateTimeString();            // 2012-01-31 00:00:00

echo $dt->addYears(5);                   // 2017-01-31 00:00:00
echo $dt->addYear();                     // 2018-01-31 00:00:00
echo $dt->subYear();                     // 2017-01-31 00:00:00
echo $dt->subYears(5);                   // 2012-01-31 00:00:00

echo $dt->addMonths(60);                 // 2017-01-31 00:00:00
echo $dt->addMonth();                    // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps
echo $dt->subMonth();                    // 2017-02-03 00:00:00
echo $dt->subMonths(60);                 // 2012-02-03 00:00:00

echo $dt->addDays(29);                   // 2012-03-03 00:00:00
echo $dt->addDay();                      // 2012-03-04 00:00:00
echo $dt->subDay();                      // 2012-03-03 00:00:00
echo $dt->subDays(29);                   // 2012-02-03 00:00:00

echo $dt->addWeekdays(4);                // 2012-02-09 00:00:00
echo $dt->addWeekday();                  // 2012-02-10 00:00:00
echo $dt->subWeekday();                  // 2012-02-09 00:00:00
echo $dt->subWeekdays(4);                // 2012-02-03 00:00:00

echo $dt->addWeeks(3);                   // 2012-02-24 00:00:00
echo $dt->addWeek();                     // 2012-03-02 00:00:00
echo $dt->subWeek();                     // 2012-02-24 00:00:00
echo $dt->subWeeks(3);                   // 2012-02-03 00:00:00

echo $dt->addHours(24);                  // 2012-02-04 00:00:00
echo $dt->addHour();                     // 2012-02-04 01:00:00
echo $dt->subHour();                     // 2012-02-04 00:00:00
echo $dt->subHours(24);                  // 2012-02-03 00:00:00

echo $dt->addMinutes(61);                // 2012-02-03 01:01:00
echo $dt->addMinute();                   // 2012-02-03 01:02:00
echo $dt->subMinute();                   // 2012-02-03 01:01:00
echo $dt->subMinutes(61);                // 2012-02-03 00:00:00

echo $dt->addSeconds(61);                // 2012-02-03 00:01:01
echo $dt->addSecond();                   // 2012-02-03 00:01:02
echo $dt->subSecond();                   // 2012-02-03 00:01:01
echo $dt->subSeconds(61);                // 2012-02-03 00:00:00

 

Have a look at some more examples

$dt = Carbon::now();

echo $dt->toDateString();               // 2015-12-19
echo $dt->toFormattedDateString();      // Dec 19, 2015
echo $dt->toTimeString();               // 10:10:16
echo $dt->toDateTimeString();           // 2015-12-19 10:10:16
echo $dt->toDayDateTimeString();        // Sat, Dec 19, 2015 10:10 AM

// ... of course format() is still available
echo $dt->format('l jS \\of F Y h:i:s A');         // Saturday 19th of December 2015 10:10:16 AM

 

All the examples are for the controller method. If you want to use carbon to convert the date in your blade file, just use the like below.

{{ \Carbon\Carbon::parse( $info->created_at )->toDayDateTimeString() }}

 

The output will look like this

OUTPUT
Sat, Dec 19, 2015 10:10 AM

 

Displaying for Humans

Sometimes we need to show 1 hour ago, 1 month ago, 3 months before, How can we do that? To do that just follow the below instructions.

$dt     = Carbon::now();
$past   = $dt->subMonth();
$future = $dt->addMonth();

echo $dt->subDays(10)->diffForHumans();     // 10 days ago
echo $dt->diffForHumans($past);             // 1 month ago
echo $dt->diffForHumans($future);           // 1 month before

 

Hope this carbon laravel tutorial can help you. Be sure to look through the official Carbon docs

 

#laravel #laravel-9x