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.
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'));
}
}
<tbody id="table_data">
@foreach ($data as $punch)
<tr>
<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>
<td>
<span class="badge bg-inverse-success">Perfect</span>
</td>
<td>0</td>
</tr>
@endforeach
</tbody>
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)) }}