简体   繁体   English

提前2天订购日期

[英]Date order 2 days ahead

$datetime = new \DateTime();


$listItem = array('<li">', '</li>');
$listItem_active = array('<li class="active-day">', '</li>');

$i = 0;
while (true) {

    if ($i === 5) break;

    if ($datetime->format('N') === '7' && $i === 0) {
        $datetime->add(new \DateInterval('P1D'));
        continue;        
    }

    if($i===0){
        $today = $datetime->format('D d-m');
    }
    if($i===3){
        echo $listItem_active[0] . $today . $listItem_active[1];
    }
    if($i!=0){
        echo $listItem[0] . $datetime->format('D d-m') . $listItem[1];
    }

    $listItem = array('<li>', '</li>');    

    $datetime->add(new \DateInterval('P1D'));

    $i++;
}

I have the above code made and its almost right, but the output is not exactly how i want it. 我有上面的代码,它几乎是正确的,但输出并不是我想要的。 I get the following output: 我得到以下输出: 在此输入图像描述

The current day should always be in the middle. 当天应始终处于中间位置。 as you can see this works. 你可以看到这个有效。 but the order of the days is not as i desire. 但是日子的顺序并不像我想的那样。 The order i desire is this : 我想要的顺序是这样的:

在此输入图像描述

You could use strtotime to get these results and the code will be much shorter. 你可以使用strtotime来获得这些结果,代码会更短。

For example: 例如:

for ($i = 2; $i > -3; $i--)
{
    echo date('D d-m', strtotime($i . ' days ago')) . '<br />';
}

will output: 将输出:

Tue 16-05
Wed 17-05
Thu 18-05
Fri 19-05
Sat 20-05

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM