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 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 halcon联合c#遇到了问题不能解决
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 写一个利用ESP32自带按键和LED控制的录像代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。