Frage

This may seem like a simple thing but I just can't get my head around it. How do I use php to display a div on a website at a specific time for only a duration? Eg. Show object for 10min every hr.

Ok, I have an object ie ahajsjajshaksjaksjiajsns which is displayed on a website when visitors visit the site. But I don't want it to show all the time but say every hr. And to disappear from the website after 10min.

War es hilfreich?

Lösung

if(date("i") < 10) echo "...";

This code will echo ... every hour from minute 0 to minute 9. e.g. 8.00-8.09, 9.00-9.09, 10.00-10.09, ...

PHPs date function is able to give you values you can check against a specific date. For more information see the docs.

Andere Tipps

I haven't coded anything web in a few years, so forgive me if I'm rusty but:

You can't do this all serverside. The page will compile but unless the user refreshes the page, the object in question will be forever available.

What you want to do, is use kekub's example to generate the following code only if it's within the time range, but also include JavaScript to destroy it when time is up:

$time = date("i");

if($time < 10){
    $timeToExpire = 10 - $time;

    echo "<div id="yourObject">I will expire soon!</div>;

    echo"<script type = 'text/javascript'>setTimeout(function() {
         $('#yourObject').fadeOut('fast');
    }, ".$timeToExpire * 10000.");</script>";  // * milliseconds e.g 6 minutes
}

I haven't tested it but what should happen is, the webpage will generate the div and also the code needed to hide it when the time is up (say there is only 6 minutes left to show it).

Although I think personally you should do this all in Javascript.

you can set time for two divs using setTimeout and can call divs in that function

setTimeout(function(){page2(mintime)},10000); setTimeout(function(){page1(mintime)},10000);

This kind of function usually should not be done in server side. Anyway if you want to achieve this using PHP you can have something like this:

while (true) {
    $result = yourfunction;
    if (resultIsGood) {
        break;
    }
    sleep(3);
}

You can sleep in a loop

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top