How to get last 30 days data in laravel? - 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();

Related

Get records between two columns using range between two dates, Also search filter criteria

I want to get records that are joined between this date period (2022-02-15, 2022-08-20)
$users = User::whereDate('start_at', '>=', $startDate)
->whereDate('end_at', '<=', $endDate)
->get();
$_start_date = '2022-02-15';
$_end_date ='2022-08-20';
$users = User::where(function ($query) use ($_start_date, $_end_date) {
$query->where(function ($query) use ($_start_date, $_end_date) {
$query->whereRaw("start_date >= date('$_start_date')")
->whereRaw("end_date <= date('$_end_date')");
})
->orwhere(function ($query) use ($_start_date, $_end_date) {
$query->whereRaw("start_date <= date('$_start_date')")
->whereRaw("end_date >= date('$_end_date')");
});
})->get();
try above code this will also return between dates of start_date and end _date e.g below
$_start_date = '2022-02-18';
$_end_date ='2022-08-20';
and also works with below dates e.g
$_start_date = '2022-02-16';
$_end_date ='2022-08-18';
codes look like this in ide
You can use the whereBetween Laravel function:
$users = User::whereBetween('start_at', [ $startDate, $endDate])->get();
Note $startDate and $endDate must be instance of Carbon.
$startDate = new Carbon('paste_your_start_date')->format('Y-m-d')." 00:00:00";
Update
This would be one method to get data from two seperated columns. Example is not tested.
$start_at = Carbon::createFromFormat('Y-m-d H', '2022-02-15 0')->toDateString();
$end_at = Carbon::createFromFormat('Y-m-d H', '2022-08-20 0')->toDateString();
$users = User::whereRaw("start_at <= date('$start_at')")
->whereRaw("end_at >= date('$end_at')")
->get();
Depends on what you need exactly
User::whereDate('start_date', '>=', $startDate)->where('end_date', '<=', $endDate)->get();
or
User::whereDate('start_date', '<', $User)->whereDate('end_date', '>', $startDate)->get();
I think you want to get the records that at least one day from start_date to end_date that exists between 2022-02-15 and 2022-08-20:
$startDate = '2022-02-15';
$endDate = '2022-08-20';
$users = User::whereDate('start_at', '<=', $endDate) // where or whereDate also should work
->whereDate('end_at', '>=', $startDate)
->get();

Compare date time in Laravel eloquent

I have start and end date time column in database
start_date end_date contact
2021-01-25 17:30:00 2021-01-25 20:30:00 xxxxxxxxx
2021-01-27 17:30:00 2021-01-28 19:30:00 xxxxxxxxx
Now i'm trying to get records which exists between these two date time, it should match mobile number also.I'm using below query but it is not filtering record
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '>=', $date)->where('end_date', '<=', $date);})->where('contact',$contact_number)->paginate(5);
You have to invert the >= and <= signs
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number = $request->contact_number;
$event = Event::where(function ($query) use ($date, $contact_number){
$query->where('start_date', '<=', $date)
->where('end_date', '>=', $date);
})
->where('contact',$contact_number)
->paginate(5);
Laravel 4+ offers you these methods: whereDay(), whereMonth(), whereYear() and whereDate() .
Here are some ways to make the comparison :
->where('date', '<=', '2014-07-10 23:59:59')
->where('date', '<', '2014-07-11')
// '2014-07-11'
$dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
->where('date', '<', $dayAfter)
Try this:
$date = Carbon\Carbon::parse($request->date)->format('Y-m-d H:i:s');
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '<=', $date)->where('end_date', '>=', $date);})->where('contact',$contact_number)->paginate(5);

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

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

Resources