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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog