Reputation: 4296
I have a while loop that shows 5 days. The first day is the current day and the other ones are the next 4. This is my code:
$datetime = new \DateTime();
$listItem = array('<li class="active">', '</li>');
$i = 0;
while (true) {
if ($i === 5) break;
if ($datetime->format('N') === '7' && $i === 0) {
$datetime->add(new \DateInterval('P1D'));
continue;
}
echo $listItem[0] . $datetime->format('D d-m') . $listItem[1];
$listItem = array('<li>', '</li>');
$datetime->add(new \DateInterval('P1D'));
$i++;
}
My problem is, I want the current day to be in the middle. Where saturday is should be the current day. Do you know how to do this?
Thanks in advance.
Upvotes: 0
Views: 63
Reputation: 6650
Please Try Below Code :
$datetime = new \DateTime();
$listItem = array('<li">', '</li>');
$listItem_active = array('<li class="active">', '</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++;
}
Upvotes: 1
Reputation: 94662
You could simply subtract 2 days from your starting day like this.
$datetime = new \DateTime();
$datetime->sub(new \DateInterval('P2D'));
Upvotes: 1
Reputation: 192
$datetime = new \DateTime();
$datetime->modify('-2 Day');
http://php.net/manual/en/datetime.modify.php
Upvotes: 1