PHP Agregue dos horas a una fecha dentro de las horas dadas usando la función
¿Cómo estructuraría las condiciones para agregar dos horas solo a las fechas entre las 8:30 de la mañana y las 18:30 de la tarde, excepto el sábado y el domingo?
En el caso de que se otorgue un tiempo cerca de la frontera (por ejemplo, a las 17:30 del martes), el tiempo restante se debe agregar al comienzo del siguiente período de tiempo "válido".
Por ejemplo: si la fecha dada fuera en martes a las 17:30, la adición de dos horas resultaría en miércoles a las 9:30 (17:30 + 1 hora = 18:30, 8:30 + el resto 1 hora = 9: 30). O si la fecha dada era en las 17:00 del viernes, el resultado sería las 9:00 del lunes (17:00 viernes + 1,5 horas = 18:30, 8:30 lunes + el resto .5 horas = 9:00)
Sé cómo simplemente añadir dos horas, de la siguiente manera:
$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date'] = $time1;
He probado esta función y funciona muy bien, excepto cuando uso una fecha como (2013-11-26 12:30) que me da (2013-11-27 04:30:00) el problema es con las 12:30
function addRollover($givenDate, $addtime) {
$starttime = 8.5*60; //Start time in minutes (decimal hours * 60)
$endtime = 18.5*60; //End time in minutes (decimal hours * 60)
$givenDate = strtotime($givenDate);
//Get just the day portion of the given time
$givenDay = strtotime('today', $givenDate);
//Calculate what the end of today's period is
$maxToday = strtotime("+$endtime minutes", $givenDay);
//Calculate the start of the next period
$nextPeriod = strtotime("tomorrow", $givenDay); //Set it to the next day
$nextPeriod = strtotime("+$starttime minutes", $nextPeriod); //And add the starting time
//If it's the weekend, bump it to Monday
if(date("D", $nextPeriod) == "Sat") {
$nextPeriod = strtotime("+2 days", $nextPeriod);
}
//Add the time period to the new day
$newDate = strtotime("+$addtime", $givenDate);
//print "$givenDate -> $newDate\n";
//print "$maxToday\n";
//Get the new hour as a decimal (adding minutes/60)
$hourfrac = date('H',$newDate) + date('i',$newDate)/60;
//print "$hourfrac\n";
//Check if we're outside the range needed
if($hourfrac < $starttime || $hourfrac > $endtime) {
//We're outside the range, find the remainder and add it on
$remainder = $newDate - $maxToday;
//print "$remainder\n";
$newDate = $nextPeriod + $remainder;
}
return $newDate;
}