Easy way to get day number of current quarter?

后端 未结 9 2153
暖寄归人
暖寄归人 2021-01-31 18:39

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 n

9条回答
  •  时光说笑
    2021-01-31 19:31

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

提交回复
热议问题