PHP Adicione duas horas a uma data dentro de horas dadas usando a função
Como eu estruturaria as condições para adicionar duas horas apenas às datas entre as 08:30 da manhã até as 18:30 da noite, excluindo sábado e domingo?
No caso em que uma hora perto da fronteira (por exemplo, 17:30 na terça-feira) é dada, a esquerda ao longo do tempo deve ser adicionada ao início do próximo período de tempo "válido".
Por exemplo: se a data indicada era 17:30 na terça-feira, a adição de duas horas resultaria em 9:30 na quarta-feira (17:30 + 1 hora = 18:30, 8:30 + o restante 1 hora = 9: 30). Ou se a data indicada era às 17:00 na sexta-feira, o resultado seria 9:00 na segunda-feira (17:00 sexta + 1,5 horas = 18:30, 8:30 segunda + o restante .5 horas = 9:00)
Eu sei como simplesmente adicionar duas horas, como segue:
$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date'] = $time1;
Eu tentei isso esta função e funciona muito bem, exceto quando eu uso uma data como (2013-11-26 12:30) ele me dá (2013-11-27 04:30:00) o problema é com 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;
}