strip_tags remove ent_quotes ejemplo php date date-arithmetic

php - remove - ¿Una forma fácil de obtener el número del día del trimestre actual?



strip_tags php ejemplo (8)

PHP proporciona formas de obtener el número del día actual del mes (fecha (''j'')), así como el número del día actual del año (fecha (''z'')). ¿Hay alguna manera de obtener el número del día actual del trimestre actual?

Así que ahora mismo, el 5 de agosto, es el día 36 del tercer trimestre.

Si no hay una manera estándar de calcular esto, ¿alguien tiene a mano un algoritmo (preferiblemente basado en PHP)?


Escribí una clase con los siguientes métodos. Disfrutar.

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


Necesitamos calcular la fecha del primer trimestre primero.

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

A continuación calculamos el 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'');


Puede usar Carbon . Tiene modificadores fáciles para getFirstOf {Mes, Año, 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;


Qué tal si:

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

Et voila :-)


Suponiendo que se refiere a un trimestre calendario (porque un año fiscal de la empresa puede comenzar en cualquier mes del año), podría confiar en la fecha (''z'') para determinar el día del año y luego mantener una serie simple de El día en que comienza cada trimestre:

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

Luego, con el día actual del año, primero puede ubicar el día de inicio más grande que sea menor o igual al día del año, y luego restar.

Tenga en cuenta que necesita diferentes números dependiendo del año bisiesto.


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

o

function date_quarter() { $month = date(''n''); if ($month <= 3) return 1; if ($month <= 6) return 2; if ($month <= 9) return 3; return 4; }


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


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