Easy way to get day number of current quarter?

后端 未结 9 2143
暖寄归人
暖寄归人 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条回答
  •  萌比男神i
    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');
    

提交回复
热议问题