Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this article we will discuss how to Convert time from 24-hour format to 12-hour AM/PM in Laravel using the strtotime function in the Laravel application.
The need to Convert time from 24-hour format to 12-hour AM/PM is that the time is displayed in 24-hour format.
In some applications, like a business, a corporate application, or a business directory, we need to add open and close business hours, so we simply store them from time to time in the database using the HTML time attribute, to convert them to standard time with am/pm we use g: i a format in the Laravel application.
Okay, let’s get straight to the steps for Getting the time format AM PM example in the Laravel application with an preview👇
resources\views\create.blade.php
<div class="d-flex gap-2"> <div class="w-25"> <span class="fw-bold ">Monday</span> </div> <div class="w-25"> <input type="time" name="from_time"> </div> <div class="w-25"> <input type="time" name="to_time"> </div> </div>
resources\views\view.blade.php
<div class="my-5"> <h2 class="h4">Working Day's</h2> <div class="table-responsive"> <table class="table w-100"> <thead class=""> <tr> <td class="text-white fw-bold">Day</td> <td class="text-white fw-bold">Open Hours</td> </tr> </thead> <tbody> <tr> <td>Monday</td> <td>{{ date('g:i a', strtotime($data->from_time ?: '-')) }} - {{ date('g:i a', strtotime($data->to_time ?: '-')) }}</td> </td> </tr> </tbody> </table> </div> </div>
Related article:- Store date, month, and year with different dropdowns in a single column in Laravel
You use time format string like this:
g
 for the 12-hour format of an hour without leading zerosi
for minutes with leading zerosa
for lowercase Ante meridiem and Post meridiemdate('g:i a', strtotime($data->from_time))
I hope that this article helped you time format AM PM example in Laravel application. You may also want to check out our guide on Confirm password validation in Laravel 10.