Laravel 5 - Check if date is today - laravel

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!

You can use isToday() method to check if date is today:
if ($event[$i]->created_at->isToday())

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

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();

Related

How to get last 30 days data in laravel?

I am querying data for last 30 days and not getting expected result. Today is 2021-01-18 and last three days date is 2021-01-18 but I am getting only this month and year data not December how can i get?
My code is
$startDate = \Carbon\Carbon::now()->subDays(30);
$endDate = \Carbon\Carbon::now();
return Auth::user()->invoices()
->whereBetween("invoice_date", [$startDate, $endDate])
->orderBy('created_at', 'DESC')
->get();
and also try other solutions like this,
$date = \Carbon\Carbon::now()->subDays(30);
return Auth::user()->invoices()
->whereDate("invoice_date", '>=', $date)
->orderBy('created_at', 'DESC')
->get();
get same output my database table data is,
and display out is
Try below code it will help you:
$date = \Carbon\Carbon::now()->subDays(30);
$year = \Carbon\Carbon::now()->year;
return Auth::user()->invoices()
->where("invoice_date", '>=', $date)
->where("invoice_date", '>=', $year)
->orderBy('created_at', 'DESC')
->get();

Laravel Eloquent whereTime() shows null

I have this query to get the events after the the current time and it shows as null. I think the whereTime condition is not working or I have a typo.
$query = Model::where('id', $request->id)
->where('status_id', 1)
->whereTime('event_date', '>=', Carbon::now())
->first();
When I don't have the whereTime condition and check the results, it shows this result
$query = Model::where('id', $request->id)
->where('status_id', 1)
// ->whereTime('event_date', '>=', Carbon::now())
->first();
dd(Carbon::now()->format('Y-m-d H:i:s'), $query->event_date);
Result
"2020-04-15 20:07:43"
"2020-05-23 18:00:00"
If your date like this "2021-03-21 13:33:52" then separate date and time like below
$date = Carbon::parse($timestamp)->format('Y-m-d');
$time = Carbon::parse($timestamp)->toTimeString();
Now you got date and time separated and write query to desire model like below:
User::where('status', 1)->whereDate('created_at', '>=', $date)
->whereTime('created_at', '>', $time)->get();
Hope this will help you. This works for me

Why won't my Laravel collection filter correctly by date?

I have
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where('date', '<', $b4)
->where('ends', '>=', $dt)
->orWhere('date', '>=', $dt)
->take(3)
->get();
All filters work except the first where. I tried it following the orWhere first, no dice. It displays items with the date in August 2018. Help?
I think you need to group your where queries ie.
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where(function ($query) use ($dt, $b4) {
$query->where('date', '<', $b4->format('Y-m-d'))
->where('ends', '>=', $dt->format('Y-m-d'));
})
->orWhere('date', '>=', $dt->format('Y-m-d'))
->take(3)
->get();
https://laravel.com/docs/5.5/queries#parameter-grouping

how whereBetween handle to this query

I want to use whereBetween to handle this query
select * from schedules where now() BETWEEN start and end
thank for your attentions
You can do like this
$schedules = DB::table('schedule')->select('id', 'name')
->whereBetween( DB::raw('now()'), [$startDate, $endDate])
->get();
You can write this query as:
$schedules = DB::table('schedules ')->select( 'id','name')
->where( DB::raw('now()'), '>=', 'startDateField' )
->where( DB::raw('now()'), '<=', 'endDateField' )
->get();
Also as per comment by #Devon, you can use whereBetween:
$schedules = DB::table('schedules ')->select( 'id','name')
->whereBetween( DB::raw('now()'), array('startDateField', 'endDateField')
->get();

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Resources