I want to show an element after a week using PHP

Asked

Viewed 41 times

-1

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

I have searched for everything that is singing and nothing. I am still a layman in it, but I learn fast.

  • Post your code so we can help

  • Your question is very vague. Edit with more details, code and images if you find it necessary.

  • I posted a generic solution, but the next questions should give more details. Fundamental read the [Tour], [Ask], [Help] and Manual on how NOT to ask questions for better use of the site.

1 answer

4

"After a week" assumes you know of some way when you 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 function date used this way returns a string, practice for literal comparisons (date entered in the code).

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


Relative date (calculated)

If you are storing the current date, you can work with timestamps, for ease:

Code receiving current date:

$agora = time();

Calculating a week later:

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

Save that amount somewhere, you can’t put it in the code of if but time start will be restarted every time, never coming to "week after".

Applying in the if:

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


Notes

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

  • in the case of time is the reverse, the mktime can be used for local spindle;

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

    7 * 24 * 60 * 60`
    

    Seven days 24 hours 60 minutes 60 seconds

Browser other questions tagged

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