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

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(CarbonCarbon::parse($demo->start)->format('d/m/Y') == CarbonCarbon::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!

Leave a Reply

Your email address will not be published. Required fields are marked *