Laravel 7 DB Query only where date range not in date range - php

I have something like this:
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '<=', $from)
->where('end', '>=', $till)
->get();
This is a nice solution form stackoverflow.
It seems works, but I need the opposite.
In my DB I have this data: start: 2020-09-27 10:00:00 end: 2020-09-27 12:00:00
I have to query that rows where for example:
$start_date(2020-09-27 9:00:00) and $end_date(2020-09-27 11:00:00) where both not in start: and end: Hopefully my question is clear enough.

If I am getting you right this is what you need
$from = min($start_date, $end_date);
$till = max($start_date, $end_date);
DB::table('booked')
->where('start', '>=', $till)
->orWhere('end', '<=', $from)
->get();

Related

Laravel whereBetween with dates why not working?

Here is my code:
$arr = Event::select('user_id', 'start', 'end')
->where('start', '!=', $from)
->whereBetween('start', [$from, $till])
->orWhere(function($query) use ($from, $till) {
$query->where('end', '!=', $till)
->whereBetween('end', [$from, $till]);
})->get();
start value: 2021-07-11 9:00:00
end value: 2021-07-11 15:00:00
$from value: 2021-07-11 10:30:00
$till value: 2021-07-11 13:00:00
I tried with Carbon as well, but the query result is nothing. What is the problem?
Also I tried with:
$arr = Event::where('start', '>', $from)
->where('start', '<', $till)
->get();
Simply not working!
The main code segment has some little flaw with
wherebetween('start', [$from, $till])
Because your start date doesn't lies between your from and till value. That returns nothing actually.
Try with your second solution. With a lil change in it.
$arr = Event::where('start', '<=', $from)
->where('end', '>=', $till)
->get();

Laravel `->whereDate` doesnt include today

Laravel ->whereDate doesnt include today.
I have 2 datetime col on my DB (start and end). I use this to query
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', $date)
->whereDate('end', '>', $date)
->get();
The $date variable is a string 2020-03-25 11:23:09 and I have an entry on my db with
start - 2020-03-18 10:44:00
end - 2020-03-25 20:59:00
Question: Why does this query does not fetch that entry. Any advice will be helpful. Thanks.
Because whereDate will change to raw sql with mysql inbuilt-method DATE():
select *
from `blas`
where date(`start`) <= '2020-03-25 11:23:09'
and date(`end`) > '2020-03-25 11:23:09'
So date(2020-03-25 20:59:00) => 2020-03-25 which is less than '2020-03-25 11:23:09'.
You can change your code to
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();
Try this
If you want use WhereDate write your query like.
$result = BlaModel::where('status', '=', "bla")
->whereDate('start', '<=', \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-18 10:44:00')->format('Y-m-d'))
->whereDate('end', '>',\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-03-25 20:59:00')->format('Y-m-d'))
->get();
Or use Simple Where like
$result = BlaModel::where('status', '=', "bla")
->where('start', '<=', $date)
->where('end', '>', $date)
->get();

How to type within today From 00:00:00 to 23:59:59 in Laravel

How to add "within today" (i.e. From 00:00:00 to 23:59:59) in Laravel ?
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', xxx,xxx)
->count();
If you want to get count of today group then you may use like this.
DB::table('groups')->where('owner_id', $u_id)
->whereDate('created_at', Carbon::today())
->count();
Or as per your requirement
$start = Carbon::now()->startOfDay(); //2019-07-27 00:00:00.000000
$end = Carbon::now()->endOfDay(); //2019-07-27 23:59:59.000000
DB::table('groups')->where('owner_id', $u_id)
->whereBetween('created_at', [$start, $end])->count();
You can use carbon for this, with start of the day and end of the day
$grpcnt = DB::table('groups')
->where('owner_id', $u_id)
->whereBetween('created_at', [ Carbon::now()->startOfDay(),Carbon::now()->endOfDay()])
->count();

Laravel 5.4 query builder Missing argument 2 when using orWhere clause

Can someone tell me why im getting this error and how to fix it please.
$lastDayPreviousMonth = date("Y-m-d", strtotime("last day of previous month"));
$firstDayPreviousMonth = date("Y-m-d", strtotime("first day of previous month"));
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query, $firstDayPreviousMonth, $lastDayPreviousMonth){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();
Im getting the following error when i run this
Missing argument 2 for App\Http\Models\Employees::App\Http\Models{closure}()
I think it has to do with the firstdaypreviousmonth and lastdaypreviousmonth parameters im passing into the orWhere clause - if i take it out i get undefined variable.
You ca n use closure using use keyword
$query = DB::table('employees')
->where('Emp_ClientId', '=', $clientId)
->where('Emp_StatusId', 1)
->orWhere(function ($query) use($firstDayPreviousMonth, $lastDayPreviousMonth,$clientId){
$query->where('Emp_DateSuspTerm', '>=', $firstDayPreviousMonth)
->where('Emp_DateSuspTerm', '<=', $lastDayPreviousMonth)
->where('Emp_ClientId', '=', $clientId);
})
->count();

Laravel: How to get count of all records created within in current week as of yesterday

I want to get count of one week old created records as of yesterday in laravel using created_at time stamp, I have:
//week date range upto current day
$name_current_day = date("l");
$name_current_week = date("Y-m-d",strtotime('monday this week')).'to'.date("Y-m-d",strtotime("$name_current_day this week"));
//query to get count
foreach($name_list as $name){
//created in week
$data[$network->name.'_week'] = Info::select( DB::raw('DATE(`created_at`) as `date`'),DB::raw('COUNT(*) as `count`'))
->where('created_at', '>', $name_current_week)
->where('name',$name->f_name)
->groupBy('date')
->orderBy('date', 'DESC')
->lists('count', 'date');
}
When I run this query, I am not getting accurate results, Is this the cirrect way to get last 7 days records in Laravel.
You need to compare date() as well, and it's easier to use Carbon, though you don't need that. It's up to you.
EDIT: your question is a bit unclear, but it seems that you don't want week-old, but only current week's results.
Anyway, this will work for you:
// week old results:
// $fromDate = Carbon\Carbon::now()->subDays(8)->format('Y-m-d');
// $tillDate = Carbon\Carbon::now()->subDay()->format('Y-m-d');
// this week results
$fromDate = Carbon\Carbon::now()->subDay()->startOfWeek()->toDateString(); // or ->format(..)
$tillDate = Carbon\Carbon::now()->subDay()->toDateString();
Info::selectRaw('date(created_at) as date, COUNT(*) as count'))
->whereBetween( DB::raw('date(created_at)'), [$fromDate, $tillDate] )
->where('name',$name->f_name)
->groupBy('date')
->orderBy('date', 'DESC')
->lists('count', 'date');
You can use Carbon for this, which makes working with dates easier in Laravel. It's included with the framework. You can then do this:
$yesterday = Carbon::now()->subDays(1);
$one_week_ago = Carbon::now()->subWeeks(1);
foreach($name_list as $name){
//created in week
$data[$network->name.'_week'] = Info::select( DB::raw('DATE(`created_at`) as `date`'),DB::raw('COUNT(*) as `count`'))
->where('created_at', '>=', $one_week_ago)
->where('created_at', '<=', $yesterday)
->where('name',$name->f_name)
->groupBy('date')
->orderBy('date', 'DESC')
->lists('count', 'date');
}

Categories