知道如何只显示当月的天数而不计算哥伦比亚国家的周六、周日和节假日的功能我发现这个对象来检测节假日。
class festivos
{
private $hoy;
private $festivos;
private $ano;
private $pascua_mes;
private $pascua_dia;
public function festivos($ano='')
{
$this->hoy=date('d/m/Y');
if($ano=='')
$ano=date('Y');
$this->ano=$ano;
$this->pascua_mes=date("m", easter_date($this->ano));
$this->pascua_dia=date("d", easter_date($this->ano));
$this->festivos[$ano][1][1] = true; // Primero de Enero
$this->festivos[$ano][5][1] = true; // Dia del Trabajo 1 de Mayo
$this->festivos[$ano][7][20] = true; // Independencia 20 de Julio
$this->festivos[$ano][8][7] = true; // Batalla de Boyacá 7 de Agosto
$this->festivos[$ano][12][8] = true; // Maria Inmaculada 8 diciembre (religiosa)
$this->festivos[$ano][12][25] = true; // Navidad 25 de diciembre
$this->calcula_emiliani(1, 6); // Reyes Magos Enero 6
$this->calcula_emiliani(3, 19); // San Jose Marzo 19
$this->calcula_emiliani(6, 29); // San Pedro y San Pablo Junio 29
$this->calcula_emiliani(8, 15); // Asunción Agosto 15
$this->calcula_emiliani(10, 12); // Descubrimiento de América Oct 12
$this->calcula_emiliani(11, 1); // Todos los santos Nov 1
$this->calcula_emiliani(11, 11); // Independencia de Cartagena Nov 11
//otras fechas calculadas a partir de la pascua.
$this->otrasFechasCalculadas(-3); //jueves santo
$this->otrasFechasCalculadas(-2); //viernes santo
$this->otrasFechasCalculadas(36,true); //Ascención el Señor pascua
$this->otrasFechasCalculadas(60,true); //Corpus Cristi
$this->otrasFechasCalculadas(68,true); //Sagrado Corazón
// otras fechas importantes que no son festivos
// $this->otrasFechasCalculadas(-46); // Miércoles de Ceniza
// $this->otrasFechasCalculadas(-46); // Miércoles de Ceniza
// $this->otrasFechasCalculadas(-48); // Lunes de Carnaval Barranquilla
// $this->otrasFechasCalculadas(-47); // Martes de Carnaval Barranquilla
}
protected function calcula_emiliani($mes_festivo,$dia_festivo)
{
// funcion que mueve una fecha diferente a lunes al siguiente lunes en el
// calendario y se aplica a fechas que estan bajo la ley emiliani
//global $y,$dia_festivo,$mes_festivo,$festivo;
// Extrae el dia de la semana
// 0 Domingo … 6 Sábado
$dd = date("w",mktime(0,0,0,$mes_festivo,$dia_festivo,$this->ano));
switch ($dd) {
case 0: // Domingo
$dia_festivo = $dia_festivo + 1;
break;
case 2: // Martes.
$dia_festivo = $dia_festivo + 6;
break;
case 3: // Miércoles
$dia_festivo = $dia_festivo + 5;
break;
case 4: // Jueves
$dia_festivo = $dia_festivo + 4;
break;
case 5: // Viernes
$dia_festivo = $dia_festivo + 3;
break;
case 6: // Sábado
$dia_festivo = $dia_festivo + 2;
break;
}
$mes = date("n", mktime(0,0,0,$mes_festivo,$dia_festivo,$this->ano))+0;
$dia = date("d", mktime(0,0,0,$mes_festivo,$dia_festivo,$this->ano))+0;
$this->festivos[$this->ano][$mes][$dia] = true;
}
protected function otrasFechasCalculadas($cantidadDias=0,$siguienteLunes=false)
{
$mes_festivo = date("n", mktime(0,0,0,$this->pascua_mes,$this->pascua_dia+$cantidadDias,$this->ano));
$dia_festivo = date("d", mktime(0,0,0,$this->pascua_mes,$this->pascua_dia+$cantidadDias,$this->ano));
if ($siguienteLunes)
{
$this->calcula_emiliani($mes_festivo, $dia_festivo);
}
else
{
$this->festivos[$this->ano][$mes_festivo+0][$dia_festivo+0] = true;
}
}
public function esFestivo($dia,$mes)
{
//echo (int)$mes;
if($dia=='' or $mes=='')
{
return false;
}
if (isset($this->festivos[$this->ano][(int)$mes][(int)$dia]))
{
return true;
}
else
{
return FALSE;
}
}
}
干杯! 您可以将PHP的DateTime类与DateInterval类一起使用,这样您就可以创建一个DatePeriod并使用 foreach 对其进行迭代。
这里我们初始化所有变量
要获取当前月份的所有日期,您可以使用 DateTime 构造函数来初始化该月的第一天和最后一天
我们使用已建立的参数创建 DatePeriod
然后在一个数组中我们分配假期
然后我们迭代
用 php 7.0 测试它会打印:
03-01-2017 03-02-2017 03-03-2017 03-06-2017 03-07-2017 03-08-2017 03-09-2017 03-10-2017 03-13-2017 03-14-2017 03-15-2017 03-16-2017 03-17-2017 03-21-2017 03-22-2017 03-23-2017 03-24-2017 03-27-2017 03-28-2017 03-29-2017 30-03-2017
做你正在寻找的一个简单的方法是将假期存储在一个数组中。
当你这样做时,你只需要得到当月的天数:
现在是时候迭代这些日子了:
在循环内部,您只需检查它是星期六、星期日还是数组中的日期,如果发生这种情况,您不显示它,而是存储它等等。如果您考虑将日期存储在数组中的方式很重要,例如以这种格式存储它:
要在循环中生成日期,您只需连接:
得到:
我已经放了年份,但最好不要放它,以便它适用于所有年份。
我希望我明白你在找什么。
一切顺利!
我正在尝试在 php 中创建一个函数,该函数允许我添加给定日期的天数,但不计算周末。现在我已经设法添加天数并根据需要显示给我,也就是说,添加天数,但每 5 年我添加 3 天,到目前为止一切正常,它告诉我它对应于多少天和多大这是。但是现在他们改变了一切,他们要求我不要连续工作,而是工作日,并以 15 天开始第一年,从入学之日起每年我都会增加一天,直到我 15 岁,如果我停止我正在尝试解决,但我不知道您是否可以指导我。
我不知道我对自己的理解是否正确。
我按我的方式放置代码以及它目前如何为我工作。