Tudor Tudor - 27 days ago 9
PHP Question

Hide links after the current day

I'm currently building Advent calendar, and as you know, only 24 days are shown. Each of the day has a link that will shown its page.

I'm trying to show links only to the pages from day 1 to current day, not after current day.

For example, If today is 20 December, the rest (21, 22, 23, 24 December should have no link, no a href)

Here's how my code looks right now:

$date = date('Y-m-d');
echo 'Current Date: '.$date.'<br>';

for ($x = 1; $x <= 24; $x++) {
echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
}


The output right now:

Current Date: 2016-11-11
1
2
3
4
5
..
..
24


Any suggestion would be appreciated.

Answer

Just compare $x to the day of the month. If it is greater than it, don't show the link.

$today = new DateTime();
$day   = $today->format('j');
echo 'Current Date: '.$today->format('Y-m-d').'<br>';

for ($x = 1; $x <= 24; $x++) {  
    if ($x <= $day) {        
        echo "<a class='grid-item' href=".'/region.php?region=Region&tag=2016-12-'.str_pad($x, 2, "0", STR_PAD_LEFT)."><em>$x</em></a><br>";
    }
    else {
        echo $x;
    }
}