Query by date and time.
I have table with field and sample data
Schedule
--date_start = 2019-05-01
--date_end = 2019-06-30
--time_start = 08:00:00
--time_end = 10:00:00
My question is can I query it by doing select date by (current_date) if current_date is in range or in between the date_start and date end. the same also in time if time is in range
thanks in advance.
You can do it with something like this
$current = Schedule::whereDate('start_date', '<=', today())
->whereDate('end_date', '>=', today())
->whereTime('start_time', '<=', now())
->whereTime('end_time', '>=', now())
->get();
$current will be the DB records that are for now.
More information at the Docs
Related
I am trying last seven days record using below query, but it does not work.
<?php
DB::table('data_table')
->select(DB::raw('SUM(fee) as counter, left(DATE(created_at),10) as date'))
->whereIn('user_id', $descendants)
->whereRaw('DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')
->groupBy(DB::raw('left(DATE(created_at),10)'))
->get();
How to make it correct?
->where('created_at', '>=', Carbon\Carbon::now()->subDays(7)->startOfDay())->get()
How to use carbon and count the users created in the last 7 days. On blade, I have a table that shows the total of users created per day that has this syntax (example is for today)
$getusertoday = MemberProfile::where('encoded_by', '=', $profile->name)
->whereDate('created_at', '=', $today )->count();
But in the last column, which is the Weekly Total, I must total everything.
$getusertoday = MemberProfile::where('encoded_by', '=', $profile->name)
->whereDate('created_at', '>=', date('Y-m-d H:i:s',strtotime('-7 days')) )->count();
I have the following MySQL query:
SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY;
When I run that in MySQL it gives me the number of records for the past day.
I'm trying to do this in Laravel, because it's included in the WHMCS software I'm using, and can't really understand how to make it work.
Sometimes, when you are stuck in Laravel, just paste your SQL into a raw query like
$result = DB::select("SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY");
Using a query builder
$result = DB::table('tblname')
->whereDate('date', '>=', Carbon::today())
->whereDate('date', '<=', Carbon::tomorrow())
->count();
You can use carbon like
$query = DB::table('your table')->whereDate('date', '>=', Carbon::today())->whereDate('date', '<=', Carbon::tomorrow())->count();
I have a query using Laravel's Eloquent and I need a where clause that will get the data from the last 8 hours since the current timestamp. I can easily do that in a raw query like:
SELECT * FROM task_tracker.tasks WHERE created_at > DATE_ADD(NOW(), INTERVAL -8 HOUR);
But how do I do that in an eloquent format? The tasks.created_at is a Datetime Format. I have this currently:
$tasks = Task::join('users', 'tasks.added_by', '=', 'users.id')
->join('users AS x', 'x.id', '=', 'tasks.assigned_to')
->select([
'tasks.id',
'tasks.task_description',
'tasks.start_timestamp',
'tasks.end_timestamp',
'tasks.status',
'users.name',
'x.name AS assign',
'tasks.created_at'
// ])->where('tasks.created_at', '>', 'DATE_ADD(NOW(), INTERVAL -8 HOUR)');
])->where(DB::raw('tasks.created_at', '=', 'DATE_ADD(NOW(), INTERVAL -8 HOUR)' ));
dd($tasks);
I tried using DB::raw and a plain encloure in a single quote (the commented line) but does not work and doesn't get any data. I am using MySQL.
DB::raw has one parameter so try whereRaw like this:
->whereRaw('tasks.created_at = DATE_ADD(NOW(), INTERVAL -8 HOUR')
I have the following query in laravel:
$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->where('tbl_users.birth_date', '>=', date("m-d")) //my error
->where('tbl_events.year', '>=', date("Y"))
->get();
I have a users table (tbl_users), with the date of the birthdays of each user. And an events table (tbl_events), which records the anniversary of each user. Thus creating a table that will get the next birthdays.
My birth_date is type "yyyy-mm-dd". The table events is the year. I want to get the next event on the current day and year.
ie I think the anniversary date must be greater than the current month and day, and the event with the top year to the current year
Format the birth_date to mm-dd first before comparing it to date('m-d'), like so:
$eventos = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
->where('tbl_events.year', '>=', date("Y"))
->get();
Update
If your tbl_events doesn't have a full date attribute, you could do this in 2 queries:
First, get the remaining events of this year:
$events_this_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->whereRaw("DATE_FORMAT( tbl_users.birth_date, '%m-%d') >= ?", array(date('m-d')))
->where('tbl_events.year', '=', date("Y")) // Get remaining events this year
->get();
Second, get the events for next years:
$events_following_year = EventsData::join('tbl_users', 'tbl_users.id_user', '=', 'tbl_events.id_user')
->where('tbl_events.year', '>', date("Y")) // Get events for upcoming years
->get();
Then merge them
$eventos = $events_this_year->merge($events_following_year);