2017-08-25 3 views
3

ここに問題があります。私は過去四半期を除いて、今年の四半期に入る必要があります。 F.e:Yii2 - 過去四半期を除いて現在の四半期をどのように取得するのですか?

私のデータベースにはフィールドcreated_atがあり、作成されたサービスのタイムスタンプは保存されています。私は過去四半期に行われたサービスを関与させる必要はありません。私はどうしたらいいですか?

私は過去四半期に行われたこれらのサービス、関与しないように、このようなSQL関数を記述しようとしています:

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

をしかし、私は私が間違っていると思います。助けてくれてありがとう。

編集:

 $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;] 
+0

uは完全なクエリ –

+0

を提供することができます私は質問を更新しました – HELPME

答えて

1

四半期のstartend日付を探す

$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'); 

あなたはまた、達成するためにmysqlのQuarter()を使用することができます

$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] 

クエリ結果。

2

サービス日付のtimestempをdbに格納していますが、今月の現在の四半期開始日と終了日を検索して使用することができます。

 $current_month = date('m'); 
    $current_year = date('Y'); 
    if($current_month>=1 && $current_month<=3) 
    { 
     $start_date = strtotime($current_year.'-01-01 00:00:00'); 
     $end_date = strtotime($current_year.'-03-31 23:59:59'); 
    } 
    else if($current_month>=4 && $current_month<=6) 
    { 
     $start_date = strtotime($current_year.'-04-01 00:00:00'); 
     $end_date = strtotime($current_year.'-06-30 23:59:59'); 
    } 
    else if($current_month>=7 && $current_month<=9) 
    { 
     $start_date = strtotime($current_year.'-07-01 00:00:00'); 
     $end_date = strtotime($current_year.'-09-30 23:59:59'); 
    } 
    else if($current_month>=10 && $current_month<=12) 
    { 
     $start_date = strtotime($current_year.'-10-01 00:00:00'); 
     $end_date = strtotime($current_year.'-12-31 23:59:59'); 
    } 

使用以下のようにクエリでこの$start_date$end_date timestemp:

$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', 'client_service.created', $start_date, $end_date] 
     ]) 
関連する問題