show only days without saturdays, Sundays and holidays

0

know of a function of how to only show the days of the current month without counting Saturdays, Sundays and holidays of the country of Colombia I found this object to detect the holidays.

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

   }    
}
    
asked by Albert Arias 21.03.2017 в 16:39
source

3 answers

1

A simple way to do what you're looking for would be to store the holidays in an array.

When you do that you just have to get the days that you have the current month:

$currentMonth = date('t');

Now it's time to iterate the days:

for($i = 1; $i <= $currentMonth; $i += 1) {

}

Inside the loop you should only check if it is Saturday, Sunday or if the day is in the array, if this happens, do not show it, store it, etc. In the if it is important that you keep in mind the way in which you store the date in the array, for example store it in this format:

01/01/2017

To generate the date in the loop you just have to concatenate:

date('d/m/Y', strtotime($i.'/'.date("m").'/'.date("Y")));

Getting:

$arrayFestivos = array('01/01/2017', '06/01/2017');
$currentMonth = date('t');
for($i = 1; $i <= $currentMonth; $i += 1) {
    if(!in_array(date('d/m/Y', strtotime($i.'/'.date("m").'/'.date("Y"))), $arrayFestivos) && (date('w') != 0 && date('w') != 6)){
        echo $i; //El día no es ni sábado, ni domingo ni festivo.
    }
}

I have put the year but it would be best not to put it to work for all years.

I hope I have understood what you are looking for.

Greetings!

    
answered by 21.03.2017 / 17:29
source
1

Greetings! You can use PHP's DateTime class with the class DateInterval to be able to create a DatePeriod and iterate it with a foreach.

Here we initialize all the variables

format = 'Y-m-d';
$startDateString = '2017-03-01';
$endDateString = '2017-03-31';
$startDateTime = DateTime::createFromFormat($format, $startDateString);
$endDateTime = DateTime::createFromFormat($format, $endDateString);
$dateInterval = new DateInterval('P1D');

To take all the dates of the current month you can use the DateTime constructor to initialize the first and last day of the month

$startDateTime = new DateTime('first day of this month');
$endDateTime = new DateTime('last day of this month');
$dateInterval = new DateInterval('P1D');

We create the DatePeriod with the established parameters

$days = new DatePeriod($startDateTime, $dateInterval, $endDateTime);

Then in an array we assign the holidays

$holidays = [
  '2017-03-20' => true
];

And then we iterate

foreach ($days as $day) {
  // Asignamos un número por cada día de la semana 6 y 7 para sábado y domingo
  $weekDay = $day->format('N');
  // Si es sábado, domingo o festivo no lo imprime
  if ($weekDay !== '6' && 
      $weekDay !== '7' && 
      !isset($holidays[$day->format('Y-m-d')])) {
    print_r($day->format('d-m-Y'));
    echo PHP_EOL;
  }
}

Tested with php 7.0 print this:

01-03-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 03-30-2017

    
answered by 22.03.2017 в 18:35
-1

I am trying to make a function in php that allows me to add the days given a date but that does not count me on weekends. Now I have managed to add the days and show me as I want to say that I add the days but every 5 years I add 3 days, until there everything works well tells me how many days it corresponds and the years it has. but now they changed everything and they ask me not to be continuous days but working days and to start the first year with 15 days and for every year from the date of admission I join one more day until I'm 15 and if I stop I'm trying to solve but I do not know if you can guide me.

I do not know if I understand well.

I put the code as I carry it and how it is currently working for me.

$fechaIngreso ='2016-02-01';
$fechaSalida ='2018-10-01';

$fechasalida = date("d/m/Y", strtotime($fechaSalida));


$fechaIngresoObj = new DateTime($fechaIngreso);
$fechaSalidaObJ = new DateTime($fechaSalida);
$intervalo = $fechaIngresoObj->diff($fechaSalidaObJ);


$anosEnLaInstitucion=$intervalo->y;


if ($anosEnLaInstitucion<5) {
$fechaEsperada = strtotime($fechaSalida."+ 30 days");
$fecha= date("d/m/Y",$fechaEsperada);
}

if (($anosEnLaInstitucion>5) and ($anosEnLaInstitucion<10)) {
$fechaEsperada = strtotime($fechaSalida."+ 33 days");
$fecha= date("d/m/Y",$fechaEsperada);
}

if (($anosEnLaInstitucion>=10) and ($anosEnLaInstitucion<15)) {
$fechaEsperada = strtotime($fechaSalida."+ 36 days");
$fecha= date("d/m/Y",$fechaEsperada);
}

if (($anosEnLaInstitucion>=15) and ($anosEnLaInstitucion<20)) {
$fechaEsperada = strtotime($fechaSalida."+ 39 days");
$fecha= date("d/m/Y",$fechaEsperada);
}

if ($anosEnLaInstitucion> 20) {
$fechaEsperada = strtotime($fechaSalida."+ 42 days");
$fecha= date("d/m/Y",$fechaEsperada);
}
?>

<?php
if ($anosEnLaInstitucion<5) {
$dias ='30';
}

if (($anosEnLaInstitucion>5) and ($anosEnLaInstitucion<10)) {
$dias ='33';
}

if (($anosEnLaInstitucion>=10) and ($anosEnLaInstitucion<15)) {
$dias ='36';
}

if (($anosEnLaInstitucion>=15) and ($anosEnLaInstitucion<20)) {
$dias ='39';
}

if ($anosEnLaInstitucion> 20) {
$dias ='42';
}
?>

Echo 'Le Corresponden '.$dias.' Dias';
Echo '<p>';
Echo 'Debe Regresar el '.$fecha;
    
answered by 14.11.2018 в 13:30