简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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