获取当前季度 php 的开始日期和结束日期 [英] get startdate and enddate for current quarter php
问题描述
我正在尝试按季度设置开始日期和结束日期.
I am trying to set a start date and end date by the quarter.
例如,我正在开发一个报告系统,我需要报告第 1 季度、第 2 季度、第 3 季度和第 4 季度的数据.
For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, quarter 3, and quarter 4.
第一季度 - 1 月 - 3 月
Quarter One - January - March
第二季度 - 4 月 - 6 月
Quarter Two - April - June
第三季度 - 七月 - 九月
Quarter Three - July - September
第四季度 - 十月 - 十二月
Quarter Four - October - December
例如,我有本月和上个月的一些案例,如下所示.
I have for example some cases for the current month, and the previous month as shown below.
case 'this_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), date("d"), date("Y")));
break;
case 'last_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 0, date("Y")));
break;
但现在我需要为本季度和上一季度添加案例,但我不确定如何实际操作以反映正确的季度范围.
But now i need to add cases for this and last quarter and I am not sure how to actually do that so it reflects the proper quarter range.
有什么想法吗?
推荐答案
在这个季度
检查这个.
case 'this_quarter':
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime('1-January-'.$current_year); // timestamp or 1-Januray 12:00:00 AM
$end_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM means end of 31 March
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM
$end_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM means end of 30 June
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM
$end_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM means end of 30 September
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM
$end_date = strtotime('1-January-'.($current_year+1)); // timestamp or 1-January Next year 12:00:00 AM means end of 31 December this year
}
break;
更新:2和上个季度
case 'last_quarter':
$current_month = date('m');
$current_year = date('Y');
if($current_month>=1 && $current_month<=3)
{
$start_date = strtotime('1-October-'.($current_year-1)); // timestamp or 1-October Last Year 12:00:00 AM
$end_date = strtotime('1-January-'.$current_year); // // timestamp or 1-January 12:00:00 AM means end of 31 December Last year
}
else if($current_month>=4 && $current_month<=6)
{
$start_date = strtotime('1-January-'.$current_year); // timestamp or 1-Januray 12:00:00 AM
$end_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM means end of 31 March
}
else if($current_month>=7 && $current_month<=9)
{
$start_date = strtotime('1-April-'.$current_year); // timestamp or 1-April 12:00:00 AM
$end_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM means end of 30 June
}
else if($current_month>=10 && $current_month<=12)
{
$start_date = strtotime('1-July-'.$current_year); // timestamp or 1-July 12:00:00 AM
$end_date = strtotime('1-October-'.$current_year); // timestamp or 1-October 12:00:00 AM means end of 30 September
}
break;
这篇关于获取当前季度 php 的开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!