我在这里遇到了问题。我需要在没有上一季度的情况下获得一年的四分之一。 F.e:
在我的数据库中,我有一个字段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 :(得分:2)
您将时间点存储在数据库中的服务日期,我们可以从当前月份找到当前的季度开始日期和结束日期,并在查询中使用。
$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
时间段,如下所示:
$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]
])
答案 1 :(得分:1)
查看start
和end
季度日期
$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');
<强>查询强>
$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]
您也可以使用mysql Quarter()来实现结果。