从Mysql Database Timestamp Laravel仅选择当前周的记录 [英] Select only records of the current week from Mysql Database Timestamp Laravel

查看:148
本文介绍了从Mysql Database Timestamp Laravel仅选择当前周的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从星期一到星期五的记录,但是我很难想像和研究如何做到这一点,我在互联网上找到了一些方法,但是很难理解对于像我这样的新手,所以这里是我的代码:

  $ myquery = DB :: table('attendances')
- > leftJoin('employees','attendances.user_id','=','employees.id')
- > where('user_id','=',$ employee-> id )

//我想做这样的事情
- > WHERE WEEK(`date`)= WEEK(NOW()

- > orderBy('attendances.date','asc') - > get();

一些这样的答案

  $ myquery = DB :: table('attendances')
- > leftJoin ('employees','attendances.user_id','=','employees.id')
- > where(WEEKOFYEAR(`date`),=,WEEKOFYEAR (NOW()))
- > where('user_id','=',$ employee-> id)
- > orderBy('attendances.logon','asc' - >得到();


解决方案

您可以使用 两个 Carbon 日期实例:

  - > whereBetween('date',[
Carbon\Carbon :: parse('last monday') - > startOfDay(),
Carbon\Carbon :: parse('next friday' ) - > endOfDay(),
])

如果要查找所有记录在这个月份,使用这个:

   - > whereBetween('date',[
Carbon\\Carbon :: now() - > startOfMonth(),
Carbon\Carbon :: now() - > endOfMonth(),
])
/ pre>

I want to get only the records from monday to friday of the current week, but i'm having a hard time thinking and researching ways on how to do it, I found some ways on the internet but its so hard to understand for a newbie like me, so here is my code:

         $myquery=DB::table('attendances')
        ->leftJoin('employees', 'attendances.user_id', '=', 'employees.id')
        ->where('user_id', '=', $employee->id)

        //I want to do something like this one
         ->WHERE WEEK(`date`) = WEEK(NOW()  

        ->orderBy('attendances.date','asc')->get();

I implement some of the answers like this

         $myquery=DB::table('attendances')
        ->leftJoin('employees', 'attendances.user_id', '=', 'employees.id')
        ->where("WEEKOFYEAR(`date`)", "=", "WEEKOFYEAR(NOW())")
        ->where('user_id', '=', $employee->id)
        ->orderBy('attendances.logon','asc')->get();

解决方案

You can use a whereBetween with two Carbon date instances:

->whereBetween('date', [
    Carbon\Carbon::parse('last monday')->startOfDay(),
    Carbon\Carbon::parse('next friday')->endOfDay(),
])

If you want to find all record in the current month, use this:

->whereBetween('date', [
    Carbon\Carbon::now()->startOfMonth(),
    Carbon\Carbon::now()->endOfMonth(),
])

这篇关于从Mysql Database Timestamp Laravel仅选择当前周的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆