Laravel 5 - 检查日期是否是今天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40256978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:39:29  来源:igfitidea点击:

Laravel 5 - Check if date is today

laraveldate

提问by WellNo

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!

谢谢!

回答by Alexey Mezenin

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

您可以使用isToday()方法检查日期是否为今天:

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

回答by kapil.dev

you can use Carbon

你可以使用碳

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

回答by Nasir Khan

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

您可以使用 whereDate、whereMonth、whereDay、whereYear、whereTime

whereDate()

日期()

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

whereMonth()

whereMonth()

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

whereDay()

whereDay()

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

whereYear()

whereYear()

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

whereTime()

时间()

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