2009-08-05 36 views
17

PHP cung cấp các cách để lấy số của ngày hiện tại của tháng (ngày ('j')) cũng như số ngày hiện tại của năm (ngày ('z')). Có cách nào để lấy số ngày hiện tại của quý hiện tại không?Cách dễ dàng để có được số ngày của quý hiện tại?

Vì vậy, ngay bây giờ, ngày 5 tháng 8, đó là ngày thứ 36 của quý thứ ba.

Nếu không có cách tính chuẩn nào, thì có ai có thuật toán (dựa trên PHP) thích hợp hơn không?

+0

Bạn xác định "quý" như thế nào? –

+0

Một nhóm ba tháng. –

+1

Mã giả: day_of_quarter (y, m, d) = day_of_year (y, m, d) - day_of_year (y, 3 * ((m-1)% 3) + 1, 1) + 1 – balpha

Trả lời

11

Tôi đã viết một lớp với các phương pháp sau. Thưởng thức.

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

Giả sử bạn có nghĩa là một quý dương lịch (vì năm tài chính của công ty có thể bắt đầu vào bất kỳ tháng nào trong năm), bạn có thể dựa vào ngày ('z') để xác định ngày trong năm và sau đó tiếp tục một mảng đơn giản trong ngày mỗi quý bắt đầu từ ngày:

$quarterStartDays = array(1 /* Jan 1 */, 90 /* Mar 1, non leap-year */, ...); 

sau đó, với sự hiện ngày trong năm đầu tiên, bạn có thể xác định vị trí bắt đầu ngày lớn nhất đó là nhỏ hơn hoặc bằng với ngày trong năm, sau đó trừ.

Lưu ý rằng bạn cần số khác nhau tùy thuộc vào năm nhuận.

1
<?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"; 
?> 
49

Làm thế nào về:

$curMonth = date("m", time()); 
$curQuarter = ceil($curMonth/3); 

Et thì đấy :-)

+9

Tôi thực sự không thể tìm ra lý do tại sao câu trả lời này đã nhận được rất nhiều upvotes vì ​​nó không trả lời câu hỏi ở tất cả - nó chỉ đơn thuần là tìm kiếm quý mà không phải là câu hỏi – DHS

+15

Nó nhận được upvotes vì ​​mọi người đến đây trong khi tìm kiếm một công thức toán học để tìm ra quý – LucasSeveryn

+1

Câu trả lời này phải là một nhận xét. – mickmackusa

6
function date_quarter() 
{ 
    return ceil(date('n', time())/3); 
} 

hoặc

function date_quarter() 
{ 
    $month = date('n'); 

    if ($month <= 3) return 1; 
    if ($month <= 6) return 2; 
    if ($month <= 9) return 3; 

    return 4; 
} 
0

Bạn có thể sử dụng Carbon nó có bổ dễ dàng cho getFirstOf {Tháng, Năm, Khu phố}()

<?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
<?php 

function trimester_day($time = null) { 
    $time = $time ? intval($time) : time(); 
    $date = intval(date("j", $time)); 
    $month = intval(date("n", $time)); 
    $year = intval(date("Y", $time)); 
    // get the selected quarter as number between 1 and 4 
    $quarter = ceil($month/3); 
    // get the first month of current quarter in n format 
    $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 is leap year 
    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 summming the relative portion of $map array 
    $map[$month-1] = $date; 
    $day = array_sum(array_slice($map, ($fmonth - 1), ($month - $fmonth + 1))); 
    // return 
    return "Day number " . $day . " on " . $total . " of quarter " . $quarter . ", $year."; 
} 

$time = strtotime("2017-01-01"); // return Day number 1 on 90 of quarter 1, 2017. 
$time = strtotime("2017-04-01"); // return Day number 1 on 91 of quarter 2, 2017. 
$time = strtotime("2017-08-15"); // return Day number 46 on 92 of quarter 3, 2017. 
$time = strtotime("2017-12-31"); // return Day number 92 on 92 of quarter 4, 2017. 

echo trimester_day($time) . "\n"; 
+0

Bạn nên cung cấp một số ví dụ đầu vào và đầu ra, cũng như giải thích về mã của bạn, thay vì chỉ bán phá giá nguồn thô. –

+0

Cảm ơn bạn, mã đã được khá nhiều tự giải thích, nhưng ở đây bạn đi. –

0

Chúng tôi cần phải tính toán thời điểm quý I đầu tiên

$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'; 

tiếp theo chúng tôi tính toán 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'); 
Các vấn đề liên quan