Reputation: 5251
I'm attempting to show a list of the days of week excluding Sunday. The first day on the list should be whatever-day-it-is-today with a CSS class of 'active'. If today = Sunday, the first list item should be Monday.
Below is the code I'm using. The problem is, this outputs the first 6 days from today (including Sunday) and then misses out the-7th-day-from-today.
Any idea what I need to change to get it to skip Sunday instead?
function daysofweek() {
$datetime = new \DateTime();
$listItem = array('<li class="active"><a href="#', '" data-toggle="tab">', '</a></li>');
$i = 0;
echo '<ul class="nav nav-tabs">';
while (true) {
if ($i === 6) break;
if ($datetime->format('N') === '7' && $i === 0) {
$datetime->add(new \DateInterval('P1D'));
continue;
}
$the_day = $datetime->format('D');
echo $listItem[0] . $the_day . $listItem[1] . $the_day . $listItem[2];
$listItem = array('<li><a href="#', '" data-toggle="tab">', '</a></li>');
$datetime->add(new \DateInterval('P1D'));
$i++;
}
echo '</ul>';
}
Upvotes: 0
Views: 310
Reputation: 181
I prefer to create a function that returns an array, instead of writing the output inside it. Here my explanation first:
time
function.date
function with value 'w'
as first argument. It will return 0
for Sunday through 6
for Saturday.(24 * 60 * 60)
. That would be 86,400 seconds or 1 days from current time. The value 24
is for the hour, the first 60
is the minute, and the last 60
is the second.date
function with value 'l'
(lowercase of letter L
) as first argument.Nuff said, here's the function:
function weekDaysFromToday() {
$days = array();
$date = time();
while (count($days) < 7) {
$date += (24 * 60 * 60);
if (!date('w', $date)) continue;
$days[] = date('l', $date);
}
return $days;
}
If you want that the current day is not repeated at the end of the list, you should change the while (count($days) < 7)
into while (count($days) < 6)
.
And here's the code to render the array using your code above:
$days = weekDaysFromToday();
$active = ' class="active"';
echo "<ul class=\"nav nav-tabs\">\n";
foreach ($days as $day) {
echo "<li$active><a href=\"#$day\" data-toggle=\"tab\">$day</a></li>\n";
$active = '';
}
echo '</ul>';
There you are, hope it helps.
Upvotes: 1
Reputation: 6722
This is the same thing in much more concise formatting:
$today = date('w');
$first = 1;
for($i=$today; $i<$today+7; $i++){
if($i%7) echo "<li ".($first ? "class='active'" : '').">".date('D', strtotime("Sunday +".($i%7)." days"))."<li>";
$first=0;
}
It works by using the modulo %7
to cycle through the week, starting from today.
Upvotes: 0
Reputation: 31644
This is a bad way to code
while(true)
This ALWAYS evaluates to true. Even with your break
this is poor syntax. Your while
should contain the logic. So here's your code, restructured. I removed the duplicate call to add()
and made it run for every iteration. The loop naturally terminates itself as well.
$i = 0;
while($i < 7) {
$the_day = $datetime->format('N');
if($the_day != 7) {
echo $listItem[0] . $the_day . $listItem[1] . $the_day . $listItem[2];
$listItem = array('<li><a href="#', '" data-toggle="tab">', '</a></li>');
}
$datetime->add(new \DateInterval('P1D'));
$i++;
}
Upvotes: 2