Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the only hours and minutes in laravel blade?

I am trying to get hours and minutes and seconds in blade, normally I use like below

{{$calllog['delivery_date']}}

but now my requirement is to get hours and minutes and seconds. here is how I tried to get

$calllog['delivery_date']->todatestring()}}

Yeah, I think I misuse for this function, can someone help me out? I am new in Laravel. Any help would be greatly appreciated.

like image 512
Soul Coder Avatar asked Jul 24 '17 16:07

Soul Coder


People also ask

How do I add hours in Laravel?

You can add hours on current date using carbon in laravel 6, laravel 7, laravel 8 and laravel 9 version. If you need to add hour or more hours in date then you can use carbon in laravel. carbon provide addHour() and addHours() method to add hours on carbon date object.

What is the advantage of Laravel blade template?

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.


2 Answers

If your variable is a DateTime, you can use this :

{{ Carbon\Carbon::parse($calllog['delivery_date'])->format('H:i:s') }}

Or simplest way (depends on your version) :

{{ $calllog['delivery_date']->format('H:i:s') }}
like image 88
Vincent Decaux Avatar answered Sep 23 '22 14:09

Vincent Decaux


If you want to get minutes and hours as separate values:

{{ $calllog['delivery_date']->minute }}
{{ $calllog['delivery_date']->hour }}

You you're looking for solution to show hours and minutes as a string, use format().

For example, this will return 02:12:

{{ $calllog['delivery_date']->format('h:i') }}

And this will return 14:12:

{{ $calllog['delivery_date']->format('H:i') }}

http://carbon.nesbot.com/docs/

like image 45
Alexey Mezenin Avatar answered Sep 23 '22 14:09

Alexey Mezenin