获得当前季度的简单方法?

时间:2009-08-05 16:51:01

标签: php date date-arithmetic

PHP提供了获取当月(日期('j'))的数量以及当年的当天数(日期('z'))的方法。有没有办法获得当前季度当天的数量?

现在,8月5日是第三季度的第36天。

如果没有标准的计算方法,有没有人有(优选基于PHP的)算法?

9 个答案:

答案 0 :(得分:72)

怎么样:

$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);

答案 1 :(得分:14)

我用以下方法编写了一个类。享受。

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

答案 2 :(得分:9)

function date_quarter()
{
    return ceil(date('n', time()) / 3);
}

function date_quarter()
{
    $month = date('n');

    if ($month <= 3) return 1;
    if ($month <= 6) return 2;
    if ($month <= 9) return 3;

    return 4;
}

答案 3 :(得分:1)

假设您指的是日历季度(因为公司会计年度可以在一年中的任何一个月开始),您可以依靠日期('z')来确定日期,然后保留每个季度开始的简单数组:

$quarterStartDays = array( 1 /* Jan 1 */, 90 /* Mar 1, non leap-year */, ... );

然后,根据当前的日期,您可以首先找到小于或等于一年中的最大开始日,然后减去。

请注意,根据闰年,您需要不同的数字。

答案 4 :(得分:1)

<?php
function day_of_quarter($ts=null) {
    if( is_null($ts) ) $ts=time();
    $d=date('d', $ts);
    $m=date('m', $ts)-1;
    while($m%3!=0) {
        $lastmonth=mktime(0, 0, 0, $m, date("d", $ts),   date("Y",$ts));
        $d += date('t', $lastmonth);
        $m--;
    }
    return $d;
}
echo day_of_quarter(mktime(0, 0, 0, 1, 1,2009));
echo "\n";
echo day_of_quarter(time());
echo "\n";
?>

答案 5 :(得分:0)

你可以使用Carbon它有简单的修饰符来获取getFirstOf {Month,Year,Quarter}()

<?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;

答案 6 :(得分:0)

<?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.

答案 7 :(得分:0)

我们需要先计算第一季度的日期

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

接下来我们计算http://php.net/manual/en/datetime.diff.php

$datetime1 = new DateTime($first_quarter_day_date);
$datetime2 = new DateTime();

$interval = $datetime1->diff($datetime2);
echo $interval->format('%a days');

答案 8 :(得分:0)

我已经注意到,该线程超出了问题的范围,这是对许多带有“ Quarter”和“ PHP”的Google搜索的第一反应。

如果您使用的是组织的ISO标准,那么如果使用的是业务应用程序,则应该使用

$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);

这是不正确的,因为ISO标准中的一年的第一天可能是12月30日或12月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 );