Do not show banner with expired date

Asked

Viewed 162 times

1

I am trying to show on my site banner s that are not expired date, I have in my bank a banner with the following dates:

The current date is: 2015-02-07

Expiration Date: 2015-02-08

The script I made is like this:

<?php if ($totalRows_rsBanner > 0) {  ?>
<?php do {
            $imagem = substr($row_rsBanner['imagem'],3);
            $url = $row_rsBanner['url'];

            $DataAtual = date("Y-m-d"); // data atual
            $timestamp_DataAtual = strtotime($DataAtual); // converte para timestamp Unix

            $DataExpiracao = $row_rsBanner['data_expiracao']; // data de expiração do anúncio
            $timestamp_DataExpiracao = strtotime($DataExpiracao); // converte para timestamp Unix

?>
            <?php if ($timestamp_DataAtual > $timestamp_DataExpiracao){ ?>
               <li><a href="<?php echo $url; ?>"><img src="<?php echo $imagem; ?>"></a></li>
            <?php } ?>
            <?php } while ($row_rsBanner = mysql_fetch_assoc($rsBanner)); ?>
<?php } ?>

The field of data_expiracao is in the format Date.

1 answer

2


His comparison signal was reversed.

One more hint: use the object \DateTime to work with dates. This makes it easier to compare dates.

<?php

if ($totalRows_rsBanner > 0) {
    do {
        $imagem        = substr($row_rsBanner['imagem'],3);
        $url           = $row_rsBanner['url'];
        $DataAtual     = new \DateTime(); // sem argumentos = data atual
        $DataExpiracao = new \DateTime($row_rsBanner['data_expiracao']); // data de expiração do anúncio
        if ($DataAtual < $DataExpiracao) { ?>
            <li><a href="<?php echo $url; ?>"><img src="<?php echo $imagem; ?>"></a></li>
        <?php }
    } while ($row_rsBanner = mysql_fetch_assoc($rsBanner));
}
  • Hello @Rodrigo Rigotti, good evening, thank you very much for the hint of using the object, it was even worth.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.