Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anonymouse703's avatar

Hi how to change the time format in view?

This code is depracated already but how to do this in Laravel 8? I want to display my start time and end time in AM and PM and the date into Month, Day, year

ex. 17:00:00 to 22:00:00 inyo 5:00 PM to 10:PM

Here is the deprecated code:

 {{$event->start->format('h:i:s A')}} to {{$event->end}}

I also try this from the document but nothing happened

 <?php echo ($event->start)->format('h:i:s A'); ?>

and also the date

ex. 2021-08-21 to August, 21, 2021

Thanks.

0 likes
16 replies
anonymouse703's avatar

yes sir that's the value... Here is the dd sir.... Also I want to display the date into something like this August,21,2021

 #original: array:9 [▼
        "id" => 1
        "date_of_event" => "2021-08-21"
        "start" => "17:00:00"
        "end" => "22:00:00"
        "location" => "Boulevard"
        "title" => "Thanks Giving"
        "description" => "All Members are invited to come and celebrate"
        "created_at" => "2021-08-03 08:19:48"
        "updated_at" => "2021-08-03 08:19:48"
      ]
SilenceBringer's avatar

@anonymouse703

{{ Carbon::parse($event->date_of_event)->format('F j, Y') }} {{ Carbon::createFromFormat('H:i:s', $event->start)->format('g:i A') }} to {{ Carbon::createFromFormat('H:i:s', $event->end)->format('g:i A') }}
anonymouse703's avatar

There's an error sir Class 'Carbon' not found (View: C:\wamp64\www\cbcc-website\resources\views\welcome.blade.php)

anonymouse703's avatar

still got the same error sir Class 'Carbon' not found (View: C:\wamp64\www\cbcc-website\resources\views\welcome.blade.php)

SilenceBringer's avatar

@anonymouse703

{{ \Carbon\Carbon::parse($event->date_of_event)->format('F j, Y') }} {{ \Carbon\Carbon::createFromFormat('H:i:s', $event->start)->format('g:i A') }} to {{ \Carbon\Carbon::createFromFormat('H:i:s', $event->end)->format('g:i A') }}
1 like
Snapey's avatar

you could create an accessor which does all this instead of having it on the view

Event.php


public function getEventStartAttribute()
{
    return Carbon::createFromFormat('Y-m-dh:i:s', $this->date_of_event . $this->start)->format('F j, Y g:i A');
}

//view

{{ $event->event_start }}
2 likes
anonymouse703's avatar

this the error of your suggestion sir

Hour can not be higher than 12 (View: C:\wamp64\www\cbcc-website\resources\views\welcome.blade.php)

ItsClassified's avatar

In your model you can cast your times as a date!

class Event extends Model
{
	// Your stuff
    protected $dates = ['start', 'end', 'date_of_event'];
	// Your stuff
}

This will cast them as Carbon DateTimes automaticly.

Next in your view you can simply call the format function like this!


{{ $event->start->format('h:i:s A') }} => "05:00:00 PM"

If you have any questions make sure to @ me!

SilenceBringer's avatar

@itsclassified dates aredeprecated and will be removed in the next releases

    /**
     * The attributes that should be mutated to dates.
     *
     * @deprecated Use the "casts" property
     *
     * @var array
     */
    protected $dates = [];
1 like
ItsClassified's avatar
Level 4

I did not know that, thanks!

To use the casts u can do this in the model


    protected $casts = [
        'start' => 'datetime',
        'end' => 'datetime',
        'date_of_event' => 'date',
    ];

    public function getEventStartAttribute()
    {
        return $this->date_of_event->format('F j, Y') . ' ' .$this->start->format('g:i A');
    }

And in the view:

{{ $event->event_start }} => "January 1, 2021 5:00 PM"

PS. @snapey came up with making the accessor..

ItsClassified's avatar

The only thing that is the same is the name of the variable, 'event_start', i combined both of our answers and made yours better, no need to be an ass.

Please or to participate in this conversation.