Easy way to get day number of current quarter?

后端 未结 9 1959
暖寄归人
暖寄归人 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:23

    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.

    0 讨论(0)
  • 2021-01-31 19:28
    <?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.
    
    0 讨论(0)
  • 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'));
    }
    
    0 讨论(0)
  • 2021-01-31 19:33
    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;
    }
    
    0 讨论(0)
  • 2021-01-31 19:33

    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');
    
    0 讨论(0)
  • 2021-01-31 19:35

    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;
    
    0 讨论(0)
提交回复
热议问题