How to Use Laravel Carbon to Format Time in AM/PM?
What is Carbon?
Carbon is a PHP extension for DateTime that provides an easy and convenient way to handle date and time. It is included by default in Laravel, so you can start using it right away without any additional setup.
In this tutorial, we’ll explore how to format time in AM/PM using Carbon in a Laravel application.
This guide is perfect for Laravel developers looking to improve their date and time manipulation skills.
Step-by-Step Guide to Formatting Time in AM/PM with Carbon
Step 1 : Install Laravel
Start by creating a new Laravel project if you don’t already have one. Let’s call our project TimeFormatterApp
:
composer create-project laravel/laravel TimeFormatterApp
Step 2 : Ensure Carbon is Installed
Carbon is included by default in Laravel, but if you need to install it separately for any reason, run the following command :
composer require nesbot/carbon
Step 3 : Using Carbon to Format Time
Carbon provides a format
method that uses PHP’s date
formatting options. Let’s dive into some examples.
composer require nesbot/carbon
1.) Getting the Current Time in AM/PM Format :
use Carbon\Carbon;
$currentTime = Carbon::now();
echo $currentTime->format('h:i A'); // Output: 03:15 PM
2.) Formatting a Specific Time :
$specificTime = Carbon::createFromTime(14, 30, 0); // 2:30 PM
echo $specificTime->format('h:i A'); // Output: 02:30 PM
3.) Parsing and Formatting a Time String :
$timeString = '2024-06-25 14:30:00';
$parsedTime = Carbon::parse($timeString);
echo $parsedTime->format('h:i A'); // Output: 02:30 PM
4.) Formatting Time in Different Time Zones :
$timeInNewYork = Carbon::now('America/New_York');
echo $timeInNewYork->format('h:i A'); // Output: 11:15 AM (Example based on current time)
Step 4 : Display Formatted Time in a Blade View
Let’s create a feature in TimeFormatterApp
to display the current time in AM/PM format in a Blade view.
1.) Creating a Controller :
Generate a new controller named TimeController
:
php artisan make:controller TimeController
In app/Http/Controllers/TimeController.php
, write the following code :
format('h:i A');
return view('showTime', compact('currentTime'));
}
}
2.) Creating the Blade View :
Create a new Blade view file at resources/views/showTime.blade.php
:
Current Time
Current Time: {{ $currentTime }}
3.) Adding a Route :
Define a route in routes/web.php
:
use App\Http\Controllers\TimeController;
Route::get('/show-time', [TimeController::class, 'showTime']);
Now, when you navigate to http://localhost:8000/show-time
, you should see the current time displayed in AM/PM format.
Additional Formatting Options with Carbon :
Carbon provides many options for customizing your date and time formats. Here are a few more examples:
1.) Full Date and Time :
echo $currentTime->format('l, F j, Y h:i A'); // Output: Tuesday, June 25, 2024 03:15 PM
2.) 12-Hour Format with Seconds :
echo $currentTime->format('h:i:s A'); // Output: 03:15:45 PM
3.) Custom Formats :
echo $currentTime->format('g:i A'); // Output: 3:15 PM (without leading zero)
Conclusion :
Using Laravel’s Carbon library for date and time formatting can greatly simplify your development process. By following this guide, you’ve learned how to format time in AM/PM format, both in your backend logic and in Blade views.
Happy coding, and continue exploring the robust features of Laravel and Carbon!