获取当前季度天数的简单方法?

发布于 2024-07-30 03:58:56 字数 162 浏览 10 评论 0原文

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

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

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

PHP provides ways to get the number of the current day of the month (date('j')) as well as the number of the current day of the year (date('z')). Is there a way to get the number of the current day of the current quarter?

So right now, August 5, it is day 36 of the third quarter.

If there is no standard way of calculating this, does anyone have a (prefereably PHP-based) algorithm handy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

半暖夏伤 2024-08-06 03:58:56

怎么样:

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

How about:

$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
咿呀咿呀哟 2024-08-06 03:58:56

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

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

I wrote a class with the following methods. Enjoy.

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'));
}
山田美奈子 2024-08-06 03:58:56
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;
}
function date_quarter()
{
    return ceil(date('n', time()) / 3);
}

or

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

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

    return 4;
}
緦唸λ蓇 2024-08-06 03:58:56

您可以使用 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;

You can use Carbon it has easy modifiers for 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;
落日海湾 2024-08-06 03:58:56

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

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

We need to calculate the date of the first quarter first

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

next we calculate the 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');
冷︶言冷语的世界 2024-08-06 03:58:56

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

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

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

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

Assuming you mean a calendar-quarter (because a company fiscal year can start in any month of the year), you could rely on the date('z') to determine the day-of-year, and then keep a simple array of the day each quarter starts on:

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

Then with the current day-of-year you can first locate the largest start-day that's less than or equal to the day-of-year, then subtract.

Note that you need different numbers depending on the leap year.

谜泪 2024-08-06 03:58:56
<?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";
?>
<?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";
?>
香草可樂 2024-08-06 03:58:56
<?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.
<?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.
软糖 2024-08-06 03:58:56

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

如果您正在使用 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 );

I've noticed that this thread went a bit beyond the question, and it's the first response to many google searches with "Quarter" & "PHP" in them.

If you're working with the ISO standards of organization, which you should if you're doing a business app, then

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

Is NOT correct, because the first day of a year in the ISO standards, can be 30, or 31 December.

Instead, you should use this :

  $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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文