WellNo
WellNo

Reputation: 659

Laravel 5 - Check if date is today

I'm creating a timeline and I'm nearly finished. I want that the color of the date for each timeline event is the same beside if the date is "today".

So I need something like:

@if($event[$i]->created_at->format('d.m.Y') == *code or variable that says its today*)
     ....
@endif

But I couldn't figure out what I can do to save the todays date in a variable.. Does anybody knows a solution for this?

thanks!

Upvotes: 20

Views: 23089

Answers (3)

Nasir Khan
Nasir Khan

Reputation: 813

You can use whereDate, whereMonth, whereDay, whereYear, whereTime

whereDate()

$users = DB::table('users')
        ->whereDate('created_at', '2019-11-31')
        ->get();

whereMonth()

$users = DB::table('users')
        ->whereMonth('created_at', '10')
        ->get();

whereDay()

$users = DB::table('users')
        ->whereDay('created_at', '20')
        ->get();

whereYear()

$users = DB::table('users')
        ->whereYear('created_at', '2019')
        ->get();

whereTime()

$users = DB::table('users')
        ->whereTime('created_at', '=', '11:20:45')
        ->get();

Upvotes: 2

kapilpatwa93
kapilpatwa93

Reputation: 4411

you can use Carbon

 @if($event[$i]->created_at->format('d.m.Y') == \Carbon::today() )
 ....
 @endif

Upvotes: 6

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

You can use isToday() method to check if date is today:

if ($event[$i]->created_at->isToday())

Upvotes: 40

Related Questions