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")改为 a 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 回答
0

我因为愚蠢而在墙上撞了 3 个小时后找到了解决方案,下面是我的代码:

while ($daysloop <= $totaldays) {
$tomorrow1 = date("m/d/Y", strtotime( $tomorrow1 ."+1 days" ));
$dayofweek = date('w', strtotime($tomorrow1));

if ($dayofweek == 0 || $dayofweek == 6) {
$weekends = $weekends + 1;
}
$daysloop++;
}

if ($totaldays == 0) {
$totaldays = $totaldays - $weekends + 1;
}
else {
$totaldays = $totaldays - $weekends;
}
于 2014-03-20T08:40:31.600 回答