Laravel 9 Time Format into AM PM Using Carbon Tutorial

Reading Time: 3 minutes
1,110 Views

Inside this article we will see the concept i.e Laravel 9 Time Format into AM PM Using Carbon Tutorial. Article contains the classified information about How To Manage DateTime with Carbon in Laravel.

If you are looking for a solution i.e Laravel Carbon Time Format into AM PM in application then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Read More: How To Download File Using jQuery Example Tutorial

What is Carbon in laravel?

Laravel framework uses a simple API extension to work with the date and time called Carbon. It’s a class. Carbon provides format() method to convert time to am and pm. Here, we will use g:i A format as argument to convert am and pm time.

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

How To Format Time into AM PM Using Carbon

Open project into terminal and run this command.

$ php artisan make:controller DemoController

It will create a DemoController.php file inside /app/Http/Controllers folder.

Read More: Laravel How to Check If Blade View File Exists Tutorial

Open file and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class DemoController extends Controller
{
    public function index()
    {
        $now = Carbon::now();
        $time = $now->format('g:i A');
  
        dd($time);
    }
}

Output

8:08 AM

Concept

To get current date and time using Carbon $now = Carbon::now();

Get time in AM PM format, use $now->format(‘g:i A’)

Read More: Remove HTML Tags From String Tutorial in CodeIgniter 4

We hope this article helped you to learn Laravel 9 Time Format into AM PM Using Carbon Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.