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

User1980's avatar

How to compare only the day from date time?

Hi all,

I cannot remember how to only compare the day and not the time from blade:

@if($event->start_from == $event->start_to)

This is currently my format in blade(the above output):

@if(4th January 2023 == 4th January 2023)
    protected $casts = [
        'start_from' => 'datetime',
        'start_to' => 'datetime',
];

Thanks!

0 likes
11 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@user1980 Try this:


@if(date('j', strtotime($event->start_from)) == date('j', strtotime($event->start_to)))
    <!-- The dates have the same day of the month -->
@endif

The date function takes a format string as the first argument, and a timestamp as the second argument. The 'j' format string returns the day of the month (without leading zeros). The strtotime function converts a date string to a timestamp.

4 likes
User1980's avatar

@MichalOravec Would it actually make a difference to use Carbon or not? Just asking, what do you think? I could not reply to you right away, Laracasts has put a delay in my replies....stuck with replying to people quickly.

2 likes
User1980's avatar

@MichalOravec I am sure your solution is also working very well but I had to accept the first working solution, sorry I could not add yours as well!

Thanks again!

1 like
User1980's avatar

@MichalOravec I think you misunderstood me. Does it makes any difference in this particular example, not in general....

1 like
LaraBABA's avatar

@User1980 Yes, he misinterpreted you. I believe he assumed you agreed with the first solution because it was the first, but you agreed with the first because you don't understand why the second is better than the first. And you are asking why the second solution would be better in this case than the first one.

1 like

Please or to participate in this conversation.