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?
How about:
$curMonth = date("m", time());
$curQuarter = ceil($curMonth/3);
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'));
}
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;
}
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.
<?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";
?>
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;
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');
<?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.
来源:https://stackoverflow.com/questions/1234443/easy-way-to-get-day-number-of-current-quarter