0

土日を除く、朝8時30分から夜18時30分までの日付にのみ2時間を追加する条件をどのように構成すればよいでしょうか?

国境付近の時間 (たとえば、火曜日の 17:30) が指定されている場合、残りの時間は次の「有効な」時間の開始に追加する必要があります。

たとえば、指定された日付が火曜日の 17:30 の場合、2 時間追加すると、水曜日の 9:30 になります (17:30 + 1 時間 = 18:30、8:30 + 残りの 1 時間 = 9: 30)。または、指定された日付が金曜日の 17:00 の場合、結果は月曜日の 9:00 になります (金曜日の 17:00 + 1.5 時間 = 18:30、月曜日の 8:30 + 残りの 0.5 時間 = 9:00)。

次のように、単純に 2 時間を追加する方法を知っています。

$idate1 = strtotime($_POST['date']);
$time1 = date('Y-m-d G:i', strtotime('+120 minutes', $idate1));
$_POST['due_date']  = $time1;

私はこのこの機能を試してみましたが、( 2013-11-26 12:30 ) 彼が私に ( 2013-11-27 04:30:00 ) のような日付を使用する場合を除いて、うまく機能します。問題は 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;
}
4

1 に答える 1

0

まだこれが必要かどうかはわかりませんが、とにかくここにあります。PHP 5.3 以降が必要です

<?php
function addRollover($givenDate, $addtime) {
    $datetime = new DateTime($givenDate);
    $datetime->modify($addtime);

    if (in_array($datetime->format('l'), array('Sunday','Saturday')) || 
        17 < $datetime->format('G') || 
        (17 === $datetime->format('G') && 30 < $datetime->format('G'))
    ) {
        $endofday = clone $datetime;
        $endofday->setTime(17,30);
        $interval = $datetime->diff($endofday);

        $datetime->add(new DateInterval('P1D'));
        if (in_array($datetime->format('l'), array('Saturday', 'Sunday'))) {
            $datetime->modify('next Monday');
        }
        $datetime->setTime(8,30);
        $datetime->add($interval);
    }

    return $datetime;
}

$future = addRollover('2014-01-03 15:15:00', '+4 hours');
echo $future->format('Y-m-d H:i:s');

実際に見る

何が起こっているのかについての説明は次のとおりです。

  1. まず、開始日時を表す DateTime オブジェクトを作成します

  2. 次に、指定された時間をそれに追加します (サポートされている日付と時刻の形式を参照してください) 。

  3. 週末か、午後 6 時以降か、または 30 分以上経過した午後 5 時 (例: 午後 5 時 30 分以降) かどうかを確認します。

  4. もしそうなら、datetime オブジェクトを複製して 5:30PM に設定します。

  5. 次に、終了時刻 (5:30PM) と変更時刻の差をDateInterval オブジェクトとして取得します。

  6. その後、翌日に進みます

  7. 翌日が土曜日の場合は翌日に進みます

  8. 翌日が日曜日の場合は翌日に進みます

  9. 次に、時間を午前 8 時 30 分に設定します。

  10. 次に、終了時刻 (午後 5 時 30 分) と変更時刻の差を datetime オブジェクトに追加します。

  11. 関数からオブジェクトを返します

于 2014-01-04T02:57:36.413 に答える