Where date time is? - mysql

I have Mysql filed date with datetime format.
How can I do where request comparing only date without time part?
I mean the following:
date = 2016-04-45 11:00:00
When I do request I should get row above:
where("date", "=", "2016-04-45")

$q->where(DB::raw("DATE(created_at) = '".date('Y-m-d')."'"));
$q->whereDate('created_at', '=', date('Y-m-d'));
with Carbon
$q->whereDate('created_at', '=', Carbon::today()->toDateString());
$q->whereDay('created_at', '=', date('d'));
$q->whereMonth('created_at', '=', date('m'));
$q->whereYear('created_at', '=', date('Y'));

whereRaw('date(created_at) = ?', [Carbon::now()->format('Y-m-d')] )

Related

Laravel MySQL groupby created_at with date format without time

I have a table column named created_at type datetime.
and I want to use groupby for created_at with date only and without time.
Here's my existing code :
$data = BloodPressureHistory::where('user_id', $id)
->groupBy('created_at')
->orderBy('created_at', 'DESC')
->paginate(7);
Use groupBy(DB::raw()) and DATE_FORMAT like this:
$data = BloodPressureHistory::where('user_id', $id)
->groupBy(DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d")'))
->orderBy('created_at', 'DESC')
->paginate(7);
$data = BloodPressureHistory::where('user_id', $id)
->groupBy(function ($item){
return \Illuminate\Support\Carbon::parse($item->created_at)->format('Y-m-d');
})
->orderBy('created_at', 'DESC')
->paginate(7);

How to get total users average time passed 7 days

I want to get the per day total users average usage time for passed 7 days i written the the SQL for each users average time it's coming perfectly but i have an u\issues in LARAVEL SQL function please help fix this SQL.
$currentTime = Carbon::today();
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy('acu_name')
->get();
You need to use GROUP BY for this, e.g.:
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy('acu_name', DB::raw('DATE(acu_at)'))
->get();
update
If you just need daily average for all the users, you can remove acu_name from group by, e.g.:
$userUsage = DB::table('active_user')
->select(DB::raw('acu_name as name'),
DB::raw('u_fname as fname'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('count(*) as number'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy(DB::raw('DATE(acu_at)'))
->get();
Correct Answer is
$userUsage = DB::table('active_user')
->select(DB::raw('DATE(acu_at) as date'),
DB::raw('AVG(TIMESTAMPDIFF(MINUTE,acu_at,acu_et)) as averageTime'),
DB::raw('COUNT(DISTINCT `acu_name`) as users'))
->join('u_info_one', 'active_user.acu_name', '=', 'u_info_one.u_email')
->whereDate('acu_at', '<=', $currentTime)
->groupBy(DB::raw('DATE(acu_at)'))
->orderBy(DB::raw('DATE(acu_at)'))
->get();
foreach($userUsage as $usUa)
{
$avegTime = ($usUa->averageTime/$usUa->users);
echo "['".$usUa->date."',".$avegTime."],";
}

Laravel : checking the time by whereBetween clause

i want to check if the time entered is existed already in the db or not? here in $dbsum and $dbesum i am doing the sum like if i enter starttime 11:30 then it will give 690 in $dbsum and if i enter endtime 12.30 then it will give 750 in $dbesum. 'c_start' and 'c_end' is the sum that already existed in the database.Now i want to check if $dbsum and $dbesum existed between 'c_start' and 'c_end' or not. my query is like below in controller. but it's not working.
$dbstime = explode(':',$request->input('starttime'));
$dbetime = explode(':',$request->input('endtime'));
$dbstime[0] = $dbstime[0]*60;
$dbetime[0] = $dbetime[0]*60;
$dbsum = array_sum($dbstime);
$dbesum = array_sum($dbetime);
$users=DB::table("bookings")
->select("bookdate")
->where([
['bookdate', '=', $request->input('bookdate')],
['roomname', '=', $request->input('roomname')],
['starttime', '=', $request->input('starttime')],
['endtime', '=', $request->input('endtime')],
->whereBetween('c_start', [$dbsum, $dbesum])
->whereBetween('c_end', [$dbsum, $dbesum])
->get();
$users=DB::table("bookings")
->select("id")
->where('bookdate', '=', $request->input('bookdate'))
->where('roomname', '=', $request->input('roomname'))
->where(function ($query){
$query->where($dbsum, '>', 'c_start');
$query->orWhere($dbsum, '<', 'c_end');
})
->orWhere(function($query){
$query->where($dbesum, '>', 'c_start');
$query->orWhere($dbesum, '<', 'c_end');
})
->get();

Select month only from Datetime in Laravel

I have a trouble in selecting month part only from the datetime. I have a column named payment_date in table transaction. I want to select * from transaction and group by month of payment_date. I have try with this code :
$selectedYearTrx = $connectDB->table('transaction')
->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id')
->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
->whereYear('payment_date', '=', $year)
->where('status','=','S')
->wherein('payment_channel_id', [1,2,3])
->where('currency','=','IDR')
->orderBy('id', 'desc')
->get()
->groupBy('payment_channel_id','id','monthPayment');
But, I'm getting error in monthPayment in group by. The error is call a member function groupby().
Can anyone help me to solve this problem?
Thank you.
You should put ->get() at the end of the code )
$selectedYearTrx = $connectDB->table('transaction')
->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id')
->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
->whereYear('payment_date', '=', $year)
->where('status','=','S')
->wherein('payment_channel_id', [1,2,3])
->where('currency','=','IDR')
->orderBy('id', 'desc')
->get()
->groupBy('payment_channel_id','id','monthPayment');
should be
$selectedYearTrx = $connectDB->table('transaction')
->join('payment_channel','transaction.payment_channel_id','=','payment_channel.id')
->select('id','payment_date', 'payment_channel_id','amount',DB::raw('MONTH(payment_date) as monthPayment'))
->whereYear('payment_date', '=', $year)
->where('status','=','S')
->wherein('payment_channel_id', [1,2,3])
->where('currency','=','IDR')
->orderBy('id', 'desc')
->groupBy('payment_channel_id','id','monthPayment')->get();

Kohana ORM select records by date

I need to get all rows on the specified day, using Kohana ORM. Date field name is 'created_date', stored date format is '0000-00-00 00:00:00' ('Y-m-d H:i:s').
$day = '2015-07-17'; // for example
$items_filtered = ORM::factory($this->_object_name)
->where(DB::expr("DATE_FORMAT('created_date', '%Y-%M-%d')"), '=', $day)
->order_by('created_date', 'DESC')
->find_all();
Thanks in advance!
You would want to use a between query like below:
$dayst = '2015-07-17 00:00:00'
$dayen = '2015-07-17 23:59:59'
$items_filtered = ORM::factory($this->_object_name)
->where('created_date', 'between', array($dayst, $dayen))
->order_by('created_date', 'DESC')
->find_all();