doudong7256 2017-08-25 06:34
浏览 64
已采纳

Yii2 - 如何在没有上一季度的情况下获得当前季度?

I've a problem here. I need to get a quarter of the year without the past quarter. F.e:

In my database I have a field created_at, which saves the timestamp of service created. I need to not involve those services, which was made in the past quarter. How should I do that?

I'm trying to write a SQL function like this to not involve those services, which was made in the past quarter:

$services= Service::find()
        ->where([         
            'client_service.created' => function ($model) {
                 return ceil(date('n') / 3) - 4;

But I guess I'm wrong. Thanks for any help.

Edited:

        $services= Service::find()
        ->select(['client.id as client_id'])
        ->joinWith('client')
        ->where([         
            'service.type' => Service::TYPE,
            'service.is_archived' => Service::ARCHIVED,])
        ->andWhere([
            'or',
            ['client_service.status' => ClientService::STATUS_NEGATIVE],
            [client_service.created' => function ($model) {
                 return ceil(date('n') / 3) - 4;]
  • 写回答

2条回答 默认 最新

  • dongzhong5833 2017-08-25 07:14
    关注

    Find start and end date of quarter

    $date =  new \DateTime(); // Current Date and Time
    $quarter_start = clone($date);
    
    // Find the offset of months
    $months_offset = ($date->format('m') - 1) % 3;
    
    // Modify quarter date
    $quarter_start->modify(" - " . $months_offset . " month")->modify("first day of this month");
    
    $quarter_end = clone($quarter_start);
    $quarter_end->modify("+ 3 month");
    
    $startDate = $quarter_start->format('Y-m-d');
    $endDate = $quarter_end->format('Y-m-d');
    

    Query

    $services= Service::find()
        ->select(['client.id as client_id'])
        ->joinWith('client')
        ->where([         
            'service.type' => Service::TYPE,
            'service.is_archived' => Service::ARCHIVED,])
        ->andWhere([
            'or',
            ['client_service.status' => ClientService::STATUS_NEGATIVE],
            ['between', 'date_format(FROM_UNIXTIME(client_service.created), "%Y-%m-%d")', $startDate, $endDate]
    

    You can also use mysql Quarter() to achieve the result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器