I want to show an element after a week using PHP

-1

I think there is a coda using the if expression in the style <?php if (mostrar depois de uma semana()): ?> Elemento aqui <?php endif; ?>

I searched for everything that is singing and nothing. I'm still a layman, but I'm a quick learner.

    
asked by anonymous 06.04.2018 / 17:34

1 answer

4

"After a week" assumes that you know of some way when it started counting this week.

Literal date


Having the desired date, the base code is this:

<?php
    if( date( 'Y-m-d' ) >= '2018-04-20' ) {
?>
    <p>Seu HTML aqui</p>
<?php
    }
?>

The date function used in this way returns a string , practice for comparisons (date entered in the code).

Important in this context is that the date is always in year-month-day format, because if you reverse the order, the logic of >= loses its meaning.


Relative date (calculated)

If you store the current date, you can work with timestamps, to make it easier:

Code that receives the current date:

$agora = time();

Calculating a week later:

$umasemanadepois = $agora + ( 7 * 24 * 60 * 60 );

Save this value somewhere, you can not by in the code of if otherwise the time of the beginning will be restarted every time, never arriving the "week after".

Applying on if :

<?php
    if( time() >= $umasemanadepois ) {
?>
    <p>Seu HTML aqui</p>
<?php
    }
?>


Notes

  • Use gmdate() if you want to ignore the local time zone;

  • In the case of time is the inverse, mktime can be used for local spindle;

  • time() returns the date in seconds, so it is necessary to calculate a week to adjust:

    7 * 24 * 60 * 60'
    

    Seven days × 24 hours × 60 minutes × 60 seconds

06.04.2018 / 19:19