Compare date time in Laravel eloquent - laravel

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

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

Laravel eloquent where TIMESTAMP time is specific hour

I want to check after my orWhereColumn if planned_completion_date time is at 09:00. planned_completion_date is of timestamp datatype. How can I check this? This is how my query looks like:
$portings = Porting::with('items')
->where('status', AbstractPorting::STATUS_ACCEPTED)
->where(function ($q) {
$q->where('planned_completion_date', '<=', date("Y-m-d H:i:s", time()));
$q->orWhereColumn('planned_completion_date', '<', 'first_possible_date');
})->get();
You can try this :
$portings = Porting::with('items')
->where('status', AbstractPorting::STATUS_ACCEPTED)
->where(function ($q) {
$q->where('planned_completion_date', '<=', date("Y-m-d H:i:s", time()));
$q->orWhereColumn('planned_completion_date', '<', 'first_possible_date');
$q->where(DB::raw("DATE_FORMAT(planned_completion_date, '%H:%i')"), '=', '09:00');
})->get();

PHP laravel mogodb - Filter documents with only date and only time (whereDate, whereTime)

composer install package jenssegers/mongodb
I have a list of documents in which there is a date field as follows
ISODate("2020-05-28T06:34:23.000Z")
how to filter out data with only date and time distinct condition
/** #var $db Jenssegers\Mongodb\Query\Builder */ $db = DB::connection('mgdb')->table('test');
$db
->whereDate('dateShot', '>=', Carbon::parse('2020-05-28'))
->whereDate('dateShot', '<=', Carbon::parse('2020-05-29'))
->whereTime('dateShot', '>=', Carbon::parse('06:00'))
->whereTime('dateShot', '<=', Carbon::parse('08:00'))
or
$db
->whereDate('dateShot', '>=', '2020-05-28')
->whereDate('dateShot', '<=', '2020-05-29')
->whereTime('dateShot', '>=', '06:00')
->whereTime('dateShot', '<=', '08:00')
or
$db
->where('dateShot', '>=', '2020-05-28 00:00:00')
->where('dateShot', '<=', '2020-05-29 23:59:59')
->whereTime('dateShot', '>=', '06:00')
->whereTime('dateShot', '<=', '08:00')
All examples above return null, with the above examples used on mysql is ok.
Thank !
Please use whereBetween
YourModel::whereBetween('created_at', [new Carbon(2020-05-28 . ' ' . '00:00:00'), new Carbon(2020-05-28 . ' ' . '23:59:59')])
->orderBy("created_at", 'desc');

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

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

Resources