How do i constantly check a if statement in php?

Nathan Bernard :

I have a php if statement to check server time, if is certain hour it runs a jquery function.

problem is it only gets fired once when page loads. i know how to it with javascript, but i need the if statement to be php cause i need to check the server time, not current time of user browser.

something like check it every 10sec would be ideal i guess

the code in the footer.php:

<?php
date_default_timezone_set('Europe/Rome'); // CET
$info = getdate();
$hour = $info['hours'];
if ($hour >= 8 && $hour <= 22){ ?>
    <script type="text/javascript">
    jQuery(document).ready(function(){
            document.getElementById("myid").innerHTML = "OPEN";
            document.getElementById("myid").setAttribute("class", "open");
    });
    </script>
<?php }else{ ?>
    <script type="text/javascript">
    jQuery(document).ready(function(){
            document.getElementById("myid").innerHTML = "CLOSED";
            document.getElementById("myid").setAttribute("class", "closed");
    });
    </script>
<?php } ?>
rickdenhaan :

Your PHP code is executed once when the page is loaded. Only when PHP is finished does the resulting HTML and Javascript get sent to the browser so your visitors have something to see.

If you want to periodically check something while a user is on your website, that's a job for Javascript and not PHP. Since you need to check the server time instead of the user's local time, your best bet is to use Ajax to call a PHP script every so often.

Judging by what you want to do, all you really need to know is what hour the server thinks it is. I personally prefer these kinds of PHP scripts to output JSON, mainly because Javascript can deal with it so easily, so a simple PHP script like this would be enough:

<?php
header('Content-Type: application/json');

date_default_timezone_set('Europe/Rome');
$info = getdate();
$hour = $info['hours'];
echo json_encode(['hour' => $hour]);

This will output a JSON object that contains the current hour according to the server, for example: {"hour":21}

You can use that relatively simply with jQuery. Wrap it in a setInterval to have it run periodically. You mentioned every 10 seconds, so that's what I'll use for this example:

<script>
setInterval(function() {
    // change "/server_time.php" to point to wherever you put that PHP script above
    jQuery.getJSON("/server_time.php", function(data) {
        if (data.hour >= 8 && data.hour <= 22) {
            document.getElementById("myid").innerHTML = "OPEN";
            document.getElementById("myid").setAttribute("class", "open");
        } else {
            document.getElementById("myid").innerHTML = "CLOSED";
            document.getElementById("myid").setAttribute("class", "closed");
        }
    });
}, 10000);
</script>

Quick disclaimer: I didn't test this code, so YMMV. It looks like it should work, but I didn't intend to fully solve your problem for you, rather to point out the way to go about it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342471&siteId=1