How can I change a column time attribute to AM and PM for the blade view?


I have a table named time_entries and the column name is time_start.

I’m inserting Carbon::now(); on it. The data stored in the column is 2021-08-26 09:51:37 and blade.php shows same result.

Model TimeEntry:

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class TimeEntry extends Model
    use HasFactory;

    public function user()
        return $this->belongsTo(User::class);

    public function getTimeStartAttribute($value)
        return Carbon::createFromFormat('H:i:s', $value)->TimeString();


namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\TimeEntry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AttendanceController extends Controller
    public function index()
        $data = TimeEntry::where('user_id', Auth::user()->id)->paginate(2);

        return view('Admin.Attendance-History-page', compact('data'));

Blade view:

<tbody id="table_data">
    @foreach ($data as $punch)
            <td>{{ $punch->user->name }}</td>
            <td>{{ $punch->user->employee_id }}</td>
            <td>19 Feb 2019</td>
            <td>{{ $punch->time_start }} </td>
            <td>{{ $punch->time_end }}</td>
            <td>9 hrs</td>
                <span class="badge bg-inverse-success">Perfect</span>

I want to know how can I format the output from 2021-08-26 09:51:37 to something like 09:51:AM in Blade?

Thank you for your help.


There is a straight forward solution for this using the PHP date() method.

{{ date('h:m A', strtotime($punch->time_start)) }}

This will output 09:51 AM

If you want to output 09:51:AM then just add : after the m and remove the space, like so:

{{ date('h:m:A', strtotime($punch->time_start)) }}

Answered By – Gass

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published