-2

我有一个送货服务,只在周一到周四取货。我只想为用户提供下一个可用的三天作为预定取件的选项。

我的代码有效,但我想知道是否有一种更“有效”的方式来编写我想要实现的目标?

$numericDay=date('N');
        if ($numericDay==1) {
            echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+1 days')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('+3 days')) . '">' . date('l - F j, Y', strtotime('+3 days')) . '</option>';
        }
        if ($numericDay==2) {
            echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+1 days')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
        }
        if ($numericDay==3) {           
            echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+1 days')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('+next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';
        }
        if ($numericDay>=4 and $numericDay<=7) {
            echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';
            echo '<option value="' . date('Ymd', strtotime('next wednesday')) . '">' . date('l - F j, Y', strtotime('next wednesday')) . '</option>';
        }

提前感谢您的任何见解!

4

6 回答 6

2

这是您需要的...

$dateTime = new DateTime();
$x = 0;
while ( $x < 3 ) {
    $dateTime->modify("+1 day");
    if ($dateTime->format("N") > 4)
        continue;
    printf("<option value=\"%s\">%s</option>\n", $dateTime->format("Ymd"), $dateTime->format("l - F j, Y"));
    $x ++;
}

输出

<option value="20130225">Monday - February 25, 2013</option>
<option value="20130226">Tuesday - February 26, 2013</option>
<option value="20130227">Wednesday - February 27, 2013</option>
于 2013-02-22T21:09:20.713 回答
2

这就是我要做的。

$offsets = array(
    1 => array(1, 2, 3),
    2 => array(1, 2, 6),
    3 => array(1, 5, 6),
    4 => array(4, 5, 6),
    5 => array(3, 4, 5),
    6 => array(2, 3, 4),
    7 => array(1, 2, 3)
);

$numericDay = date('N');
foreach ($offsets[$numericDay] as $offset)
{
    $dateFormat = $offset == 1 ? '\T\o\m\o\r\r\o\w - F j, Y' : 'l - F j, Y';
    echo '<option value="' . date('Ymd', strtotime('+' . $offset . ' days')) . '">' . date($dateFormat, strtotime('+' . $offset . ' days')) . '</option>';
}

编辑:整理了一下。

于 2013-02-22T21:16:22.620 回答
0

创建一个while循环:

$echoedDays = 0;
$dateIndex = 0
while ($echoedDays < 3) {

从明天的日期开始并检查天数。如果是周一至周四 (1-4),请回显日期并增加$echoedDays. 否则,只需增加$dateIndex并移至下一个日期。

于 2013-02-22T21:02:53.197 回答
0

这也可以工作:

 $numericDay=date('N');

    if($numericDay < 4)
        echo '<option value="' . date('Ymd', strtotime('+1 days')) . '">' . date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+1 days')) . '</option>';
    else
        echo '<option value="' . date('Ymd', strtotime('next monday')) . '">' . date('l - F j, Y', strtotime('next monday')) . '</option>';

    if($numericDay < 3)
        echo '<option value="' . date('Ymd', strtotime('+2 days')) . '">' . date('l - F j, Y', strtotime('+2 days')) . '</option>';
    else
        echo '<option value="' . date('Ymd', strtotime('next tuesday')) . '">' . date('l - F j, Y', strtotime('next tuesday')) . '</option>';

    if($numericDay < 2)
        echo '<option value="' . date('Ymd', strtotime('+3 days')) . '">' . date('l - F j, Y', strtotime('+3 days')) . '</option>';
    else
        echo '<option value="' . date('Ymd', strtotime('next wednesday')) . '">' . date('l - F j, Y', strtotime('next wednesday')) . '</option>';
于 2013-02-22T21:08:10.270 回答
0

Mod 4是你的朋友...

$days = Array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");

for($i=1;$i<=7;$i++){
  if($i > 4){ $n = 1; } else {
    $n = $i%4+1;
  }
  $o = ($n)%4+1;
  $p = ($o)%4+1;
  echo "It's ".$days[$i-1]." and the next three available days are... ".$days[$n-1].", ".$days[$o-1]." and ".$days[$p-1]."\n";
}

输出

It's Mon and the next three available days are... Tue, Wed and Thu
It's Tue and the next three available days are... Wed, Thu and Mon
It's Wed and the next three available days are... Thu, Mon and Tue
It's Thu and the next three available days are... Mon, Tue and Wed
It's Fri and the next three available days are... Mon, Tue and Wed
It's Sat and the next three available days are... Mon, Tue and Wed
It's Sun and the next three available days are... Mon, Tue and Wed
于 2013-02-22T21:25:43.247 回答
-1
$store = array(
    1 => array(
        date('Ymd', strtotime('+1 days')),
        date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+1 days'))
    ),
    array(
        date('Ymd', strtotime('+2 days')),
        date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+2 days'))
    ),
    array(
        date('Ymd', strtotime('+3 days')),
        date('\T\o\m\o\r\r\o\w - F j, Y', strtotime('+3 days'))
    ),
    'm' => array(
        date('Ymd', strtotime('next monday')),
        date('l - F j, Y', strtotime('next monday'))
    ),
    't' => array(
        date('Ymd', strtotime('next tuesday')),
        date('l - F j, Y', strtotime('next tuesday'))
    ),
    'w' => array(
        date('Ymd', strtotime('next wednesday')),
        date('l - F j, Y', strtotime('next wednesday'))
    )
);

$numericDay=date('N');
if ($numericDay==1) {
    echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
    echo '<option value="' . $store[2][0] . '">' . $store[2][1] . '</option>';
    echo '<option value="' . $store[3][0] . '">' . $store[3][1] . '</option>';
}
elseif ($numericDay==2) {
    echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
    echo '<option value="' . $store[2][0] . '">' . $store[2][1] . '</option>';
    echo '<option value="' . $store['m'][0] . '">' . $store['m'][1] . '</option>';
}
elseif ($numericDay==3) {           
    echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
    echo '<option value="' . $store['m'][0] . '">' . $store['m'][1] . '</option>';
    echo '<option value="' . $store['t'][0] . '">' . $store['t'][1] . '</option>';
}
elseif ($numericDay>=4 and $numericDay<=7) {
    echo '<option value="' . $store[1][0] . '">' . $store[1][1] . '</option>';
    echo '<option value="' . $store['t'][0] . '">' . $store['t'][1] . '</option>';
    echo '<option value="' . $store['w'][0] . '">' . $store['w'][1] . '</option>';
}
于 2013-02-22T21:06:12.127 回答