Php 获取当前季度天数的简单方法?
PHP提供了获取当月当前日期(日期('j'))和年度当前日期(日期('z'))的方法。有没有办法获取当前季度的当前日期的编号 所以现在,8月5日,是第三季度的第36天Php 获取当前季度天数的简单方法?,php,date,date-arithmetic,Php,Date,Date Arithmetic,PHP提供了获取当月当前日期(日期('j'))和年度当前日期(日期('z'))的方法。有没有办法获取当前季度的当前日期的编号 所以现在,8月5日,是第三季度的第36天 如果没有标准的计算方法,是否有人手边有(最好是基于PHP的)算法?假设您指的是一个日历季度(因为公司会计年度可以从一年中的任何月份开始),您可以依靠日期(“z”)来确定一年中的哪一天,然后保存每个季度开始日期的简单数组: $quarterStartDays = array( 1 /* Jan 1 */, 90 /* Mar 1,
如果没有标准的计算方法,是否有人手边有(最好是基于PHP的)算法?假设您指的是一个日历季度(因为公司会计年度可以从一年中的任何月份开始),您可以依靠日期(“z”)来确定一年中的哪一天,然后保存每个季度开始日期的简单数组:
$quarterStartDays = array( 1 /* Jan 1 */, 90 /* Mar 1, non leap-year */, ... );
然后,根据一年中的当前日期,您可以首先找到小于或等于一年中日期的最大开始日期,然后减去
请注意,根据闰年的不同,您需要不同的数字。我用以下方法编写了一个类。享受
public static function getQuarterByMonth($monthNumber) {
return floor(($monthNumber - 1) / 3) + 1;
}
public static function getQuarterDay($monthNumber, $dayNumber, $yearNumber) {
$quarterDayNumber = 0;
$dayCountByMonth = array();
$startMonthNumber = ((self::getQuarterByMonth($monthNumber) - 1) * 3) + 1;
// Calculate the number of days in each month.
for ($i=1; $i<=12; $i++) {
$dayCountByMonth[$i] = date("t", strtotime($yearNumber . "-" . $i . "-01"));
}
for ($i=$startMonthNumber; $i<=$monthNumber-1; $i++) {
$quarterDayNumber += $dayCountByMonth[$i];
}
$quarterDayNumber += $dayNumber;
return $quarterDayNumber;
}
public static function getCurrentQuarterDay() {
return self::getQuarterDay(date('n'), date('j'), date('Y'));
}
公共静态函数getQuarterByMonth($monthNumber){
返回楼层(($monthNumber-1)/3)+1;
}
公共静态函数getQuarterDay($monthNumber、$dayNumber、$DreamNumber){
$quarterDayNumber=0;
$dayCountByMonth=array();
$startMonthNumber=((self::getQuarterByMonth($monthNumber)-1)*3)+1;
//计算每个月的天数。
对于($i=1;$i
那么:
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
或
功能日期\u季度()
{
$month=日期('n');
如果($month您可以使用它,它对getFirstOf{month,Year,Quarter}()
我们需要先计算第一季度的日期
$current_month = date('m');
// Get first month of quarter
$new_month = (3 * floor(($current_month - 1 ) / 3)) + 1;
// Add prefix zero if needed
$new_month = substr('0' . $new_month, -2);
$first_quarter_day_date = date('Y') . '-' . $new_month . '-01';
接下来我们计算
我注意到这个帖子有点超出了问题的范围,这是对许多谷歌搜索的第一个回应,其中包含“Quarter”和“PHP”
如果您使用的是ISO组织标准,如果您使用的是商业应用程序,那么
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
不正确,因为ISO标准中一年的第一天可以是12月30日或31日
相反,您应该使用以下选项:
$current_yearly_cycle_year_number = 2019;
$current_yearly_cycle_start->setISODate( $current_yearly_cycle_year_number, 1, 1 );
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 53, 1 );
if( $current_yearly_cycle_end->format("W") !== "53" )
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 52, 1 );
$week_number_start = intval( $current_yearly_cycle_start->format( "W" ) );
$timestamp_start_quarter = ( $week_number_start === 1 ? 1 : intval( ceil( $current_yearly_cycle_start->format( "m" ) / 3 ) ) );
var_dump( $timestamp_start_quarter );
你是如何定义“季度”的?一组三个月。伪代码:季度之日(y,m,d)=年度之日(y,m,d)-年度之日(y,3*((m-1)%3)+1,1)+1(假设所有值都是基于1的)我真的不明白为什么这个答案得到了这么多的选票,因为它根本没有回答这个问题-它只是找到了一个季度,而不是问题,它是获得了选票,因为人们来到这里,同时寻找一个数学公式,为那些看起来是零指数季度的人找到了四分之一:只使用地板,而不是ceil。(已测试)您应该提供一些示例输入和输出,以及对代码的解释,而不仅仅是转储原始源代码。谢谢,代码基本上是自我解释的,但现在开始。已经5年了,但是..这返回的是季度,而不是季度中的某一天
<?php
//take current date
$now = Carbon\Carbon::now();
//modify a copy of it to the first day of the current quarter
$firstOfQuarter = $now->copy()->firstOfQuarter();
//calculate the difference in days and add 1 to correct the index
$dayOfQuarter = $now->diffInDays($firstOfQuarter) + 1;
<?php
function quarter_day($time = "") {
$time = $time ? strtotime($time) : time();
$date = intval(date("j", $time));
$month = intval(date("n", $time));
$year = intval(date("Y", $time));
// get selected quarter as number between 1 and 4
$quarter = ceil($month / 3);
// get first month of current quarter as number between 1 and 12
$fmonth = $quarter + (($quarter - 1) * 2);
// map days in a year by month
$map = [31,28,31,30,31,30,31,31,30,31,30,31];
// check if year is leap
if (((($year % 4) == 0) && ((($year % 100) != 0) || (($year % 400) == 0)))) $map[1] = 29;
// get total number of days in selected quarter, by summing the relative portion of $map array
$total = array_sum(array_slice($map, ($fmonth - 1), 3));
// get number of days passed in selected quarter, by summing the relative portion of $map array
$map[$month-1] = $date;
$day = array_sum(array_slice($map, ($fmonth - 1), ($month - $fmonth + 1)));
return "Day $day on $total of quarter $quarter, $year.";
}
print(quarter_day("2017-01-01")) . "\n"; // prints Day 1 on 90 of quarter 1, 2017.
print(quarter_day("2017-04-01")) . "\n"; // prints Day 1 on 91 of quarter 2, 2017.
print(quarter_day("2017-08-15")) . "\n"; // prints Day 46 on 92 of quarter 3, 2017.
print(quarter_day("2017-12-31")) . "\n"; // prints Day 92 on 92 of quarter 4, 2017.
$current_month = date('m');
// Get first month of quarter
$new_month = (3 * floor(($current_month - 1 ) / 3)) + 1;
// Add prefix zero if needed
$new_month = substr('0' . $new_month, -2);
$first_quarter_day_date = date('Y') . '-' . $new_month . '-01';
$datetime1 = new DateTime($first_quarter_day_date);
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a days');
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
$current_yearly_cycle_year_number = 2019;
$current_yearly_cycle_start->setISODate( $current_yearly_cycle_year_number, 1, 1 );
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 53, 1 );
if( $current_yearly_cycle_end->format("W") !== "53" )
$current_yearly_cycle_end->setISODate( $current_yearly_cycle_year_number, 52, 1 );
$week_number_start = intval( $current_yearly_cycle_start->format( "W" ) );
$timestamp_start_quarter = ( $week_number_start === 1 ? 1 : intval( ceil( $current_yearly_cycle_start->format( "m" ) / 3 ) ) );
var_dump( $timestamp_start_quarter );