Frage

Hallo schon ich verwende diesen Code von einer anderen Frage - die zwei zusätzliche Tage fügt im Fall Wochenenden EndDay

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

Diese Funktion ist Teil eines json Ausgang, der in einem jquery Kalender angezeigt wird. - Es nimmt startdate und enddate und macht sie

Ist es möglich, einen Code zu erzeugen, dass die Renditen Ausgänge, so dass, wenn es um ein Wochenende wird es ein Enddatum schafft, springt bis Montag erstellt ein Startdatum setzt sich dann, bis es die ursprüngliche enddate ??

gegeben erreicht
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
War es hilfreich?

Lösung

Ich bin nicht 100% sicher, was die Frage / Funktion wirklich tut, aber (wenn ich richtig geraten) hier ist eine Idee.

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

Die Logik fügt grundsätzlich $businessdays Wochentag auf das Startdatum; dann prüft, ob Feiertage innerhalb des Zeitraums, wenn alle Feiertage der Endtermin treten dann entsprechend erhöht wird.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top