简单的方法来获取当前季度的天数? [英] Easy way to get day number of current quarter?

查看:276
本文介绍了简单的方法来获取当前季度的天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP提供了获取当月当前日期(日期('j'))以及一年中当前日期(日期('z'))数量的方法。

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?

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

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

如果没有标准的计算方法,有人可以方便地使用(最好是基于PHP的)算法吗?

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

推荐答案

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

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

这篇关于简单的方法来获取当前季度的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆