Skip to content
Advertisement

How to use “If else” condition with date in blade template

Hi guys so I have post and comment system, So I want to show comment section if the day of the post +7 days. if they see the post on day 8 of the post created they can’t comment. Here is my code that I try but I still dont get it :

@if($comment['created_at'] <= 7)
    <a href="" class="btn orange-button">comment section</a>
@elseif($comment['created_at'] >7)
    <a href="" class="btn orange-button">Can't comment</a>
@else
    <span class="badge bg-pink">Hello</span>
@endif

I know it’s totally a dumb code, hope you guys can help me I’m still learning here

Advertisement

Answer

Laravel has Carbon library

use like this

if (Carbon::now()->diffInDays(Carbon::parse($comment['created_at'])) > 7) {
    //cant comment
}

in blade

@if (Carbon::now()->diffInDays(Carbon::parse($comment['created_at'])) > 7)
    //cant comment
@endif
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement