PHP Dodaj dwie godziny do daty w podanych godzinach za pomocą funkcji
Jak skonstruowałbym warunki, aby dodać dwie godziny tylko do dat między 08:30 rano a 18:30 wieczorem, z wyjątkiem soboty i niedzieli?
W przypadku podania czasu w pobliżu granicy (np. 17:30 we wtorek), lewy czas powinien zostać dodany na początku następnego „ważnego” okresu.
Na przykład: jeśli podana data przypada we wtorek o 17:30, to dwugodzinny dodatek spowoduje 9:30 w środę (17:30 + 1 godzina = 18:30, 8:30 + reszta 1 godzina = 9: 30). Lub, jeśli podana data była w 17:00 w piątek, wynikiem byłaby 9:00 w poniedziałek (17:00 piątek + 1,5 godziny = 18:30, 8:30 poniedziałek + reszta .5 godziny = 9:00)
Wiem, jak po prostu dodać dwie godziny w następujący sposób:
$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date'] = $time1;
wypróbowałem tę funkcję i działa świetnie, z wyjątkiem sytuacji, gdy używam daty takiej jak (2013-11-26 12:30) on daje mi (2013-11-27 04:30:00) problem dotyczy 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;
}