Domanda

Ciao già sto usando questo codice da un'altra domanda - che aggiunge due giorni in più per endday nel caso di fine settimana

function add_business_days($startdate,$buisnessdays,$holidays,$dateformat){
  $i=1;
  $dayx = strtotime($startdate);
  while($i < $buisnessdays){
   $day = date('N',$dayx);
   $datex = date('Y-m-d',$dayx);
   if($day < 6 && !in_array($datex,$holidays))$i++;
   $dayx = strtotime($datex.' +1 day');
  }
  return date($dateformat,$dayx);
 }

Questa funzione forme parte di un'uscita json che viene visualizzato in un calendario jquery -. Raccoglie startdate e enddate e rende

E 'possibile creare un codice che restituisce uscite in modo tale che quando si arriva a un week-end si crea una data di fine, salta a Lunedi crea una data di inizio, allora continua fino a raggiungere l'originale dato enddate ??

x = date('w');
if (x != 6) {
while (x != 6) {
//start adding days to start date
}
} else {
//create new enddate = current iteration of dates currentdate;

//then new start (add two days to it to get to monday) currentdate + 2 = newstartdate
//redo above till you get to original end date
È stato utile?

Soluzione

Non sono sicuro al 100% di quello che la domanda / funzione è davvero facendo, ma (se ho indovinato correttamente) ecco un'idea.

function add_business_days($startdate, $businessdays, $holidays, $dateformat)
{
    $start = new DateTime($startdate);
    $date  = new DateTime($startdate);
    $date->modify("+{$businessdays} weekdays");

    foreach ($holidays as $holiday) {
        $holiday = new DateTime($holiday);
        // If holiday is a weekday and occurs within $businessdays of the $startdate
        if ($holiday->format('N') < 6 && $holiday >= $start && $holiday <= $date) {
            $date->modify("+1 weekday");
        }
    }
    return $date->format($dateformat);
}

La logica aggiunge fondamentalmente giorni feriali $businessdays della data di inizio; poi verifica la presenza di eventuali vacanze entro l'intervallo di date, se tutte le feste accadono quindi la data finale viene incrementato a seconda del caso.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top