Matt Stenson

This is the personal site of Matt Stenson. I'm a programmer, nerd, mountain biker, dirt biker and outdoors enthusiest. I'll blog about different stuff here. I like open software and privacy focused tools.

Display Date and Time of Single Day or Multi Day Events With Blade

Reading Time: 1 min

I recently needed to display the date and time of a start and end date for a Laravel project. Up front I don’t know if the start and end date/time are on the same day or a different day. I was displaying the whole date twice but this looked clunky.

So, to solve this problem I added a bit of blade template logic to control how my two dates were shown. If the dates were on the same day, only show the time of the second date. But if they are are different days show the full date / time strings.

Here’s the sample blade logic.

@if(Carbon\Carbon::parse($demo->start)->format('d/m/Y') == Carbon\Carbon::parse($demo->end)->format('d/m/Y'))
<p class="text-grey-darker mb-2 group-hover:text-white no-underline">to </p>
@else
<p class="text-grey-darker mb-2 group-hover:text-white no-underline">to </p>
@endif

As you can see it is super simple, but it is a nice enhancement to the display of these dates.

Cheers!

See Also