23

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

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

如果没有标准的计算方法,是否有人手边有(最好是基于 PHP 的)算法?

4

9 回答 9

82

怎么样:

$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
于 2011-02-24T20:36:41.687 回答
17

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

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'));
}
于 2009-08-05T17:44:47.200 回答
13
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;
}
于 2012-12-15T23:07:45.640 回答
3

您可以使用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;
于 2017-02-19T12:40:29.583 回答
1

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

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

然后使用当前日期,您可以首先找到小于或等于该日期的最大开始日期,然后减去。

请注意,您需要不同的数字,具体取决于闰年。

于 2009-08-05T17:02:46.503 回答
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";
?>
于 2009-08-05T17:56:02.870 回答
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.
于 2017-04-21T17:51:01.933 回答
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');
于 2017-06-23T09:35:25.097 回答
0

我注意到这个帖子有点超出了这个问题,这是对许多带有“Quarter”和“PHP”的谷歌搜索的第一反应。

如果您正在使用 ISO 组织标准,如果您正在开发商业应用程序,那么您应该这样做

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

不正确,因为 ISO 标准中一年的第一天可以是 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 );
于 2019-12-30T14:00:18.303 回答