1

誰かが金曜日を選択すると、月曜日までの日数が自動的に追加されるようにします。$leavefrom2014 年 3 月 1 日が木曜日で、 2014 年 3 月 2 日が金曜日であると想像してください$leaveto$totaldays日付に基づいて計算されます。したがって、2日です。

<?php
$x = 0;

$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);

while ($x < $totaldays) {

    $tomorrow = date('l', strtotime($date1 ."+1 days"));

    //$tomorrow = date("m-d-Y", strtotime( $date1 ."+1 days" ));
    $getday = date('D', strtotime($tomorrow));
    $x++;
    if ($getday == "Sunday" || $getday = "Saturday") {
        $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
    }
    $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}

echo $tomorrow;
?>
4

3 に答える 3

1

単に週末をスキップしようとしている場合$date2は、週末かどうかを確認してください。そうであれば、次の月曜日にスキップしてください。

$date2 = DateTime::CreateFromFormat('n-j-Y', $leaveto);
if (in_array($date2->format('l'), array('Sunday', 'Saturday'))) {
    $date2->modify('next Monday');
}
echo $date2->format("m/d/Y");
于 2014-03-18T15:32:10.013 に答える
0

代わりに を に変更してみてif ($getday == "Sunday" || $getday = "Saturday")、最後の を削除してください。このようなもの:while$tomorrow = ...

<?php
$x = 0;

$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);

while ($x < $totaldays) {

    $tomorrow = date('l', strtotime($date1 ."+1 days"));
    $x++;

    $getday = date('D', strtotime($tomorrow));
    while ($getday == "Sunday" || $getday = "Saturday") {
        $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
        $getday = date('D', strtotime($tomorrow));
    }

}

echo $tomorrow;
?>
于 2014-03-18T15:32:14.103 に答える