Moyen facile d'obtenir numéro du jour du trimestre en cours?

PHP fournit des moyens pour obtenir le numéro du jour du mois (date('j')) ainsi que le nombre de jour courant de l'année (date('z')). Est-il possible d'obtenir le numéro du jour du trimestre en cours?

donc maintenant, le 5 Août, c'est le jour 36 du troisième trimestre.

S'il n'y a pas de méthode de calcul standard, est-ce que quelqu'un a un algorithme (basé préférablement sur PHP) à portée de main?

18
demandé sur Chad Johnson 2009-08-05 20:51:01

8 réponses

Que Diriez-vous de:

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

Et le tour est joué :-)

61
répondu Michiel 2013-03-28 10:44:29

j'ai écrit un cours avec les méthodes suivantes. Profiter.

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'));
}
14
répondu Chad Johnson 2009-08-05 17:44:47
function date_quarter()
{
    return ceil(date('n', time()) / 3);
}

ou

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

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

    return 4;
}
9
répondu Mike 2012-12-15 23:07:45

en supposant que vous voulez dire un trimestre civil (parce qu'un exercice d'entreprise peut commencer dans n'importe quel mois de l'année), vous pouvez compter sur la date ('z') pour déterminer le jour de l'année, et puis garder un tableau simple du jour chaque trimestre commence le:

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

ensuite, avec le jour de l'année en cours, vous pouvez d'abord trouver le plus grand jour de début qui est inférieur ou égal au jour de l'année, puis soustraire.

notez que vous avez besoin de nombres différents selon l'année bissextile.

1
répondu Jason Cohen 2009-08-05 17:02:46
<?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";
?>
1
répondu scompt.com 2009-08-05 17:56:02

Vous pouvez utiliser carbone il a des modificateurs faciles pour getFirstOf{mois, année, trimestre} ()

<?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
répondu Eelke van den Bos 2017-02-19 12:40:29
<?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
répondu Simo Manz 2017-04-21 21:31:24

nous devons calculer la date du premier trimestre premier

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

ensuite, nous calculons le 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
répondu Clemens Tolboom 2017-06-23 09:35:25