Search code examples
phptimehyperlinkdelay

Refresh page element after specific time


I'm not very good at code so please use laymans terms in answering!!

I have broadcast site where I hide links until a specific time of day (i.e. broadcast time) this is the code I use to do this:

<?php
date_default_timezone_set('Europe/London');
$currtime = date('Hi');
// Change time
$starttime = 2200;  
$endtime = 2300; 
$endtime = $endtime + 2400;
$endtest = $currtime + 2400;

?>
<?php

if( $currtime >= $starttime && $endtest <= $endtime ){
?>
<a href="*broadcastlink*">LIVE NOW</a>
<?php
}
else{
?>
Live at 22:00
<?php
}
?>

Currently users have to refresh the page manually which is a bit messy.

I know this could be done easily using javascript to refresh the whole page every x seconds but I don't want to do this as I have another piece of code that shows a "facebook like" popup window that doesn't close for 5 minutes unless they click like, so refreshing the whole page would constantly show the popup window.

Please can someone write a script so that I can link a refresh script to the php script above so that at the link reveal time the container refreshes, I've looked around and found nothing suitable so far?

Thanks in advance for any help - much appreciated


Solution

  • bad news is PHP cannot force browser to do anything. you need javascript with ajax to achieve this. but what if javascript is disabled in client side. there is a small trick in php .

    here is what you can do.

    determine the number of seconds for the live broadcast to start by doing

    $numOfSeconds = $starttime - $currtime;
    

    then once you know after how many seconds you want to refresh your browser here is how you can do it with php.

    header( "refresh:$numOfSeconds;urltoanywhere.php" );
    

    this will take you to any url you wish to go after specific time. define the url to the same page if you wish to refresh. and you could extend this logic to implement the way you want to.

    Update1:

    Below is the working code which i have tested. hope this helps you :)

    <?php
    ob_start();
    date_default_timezone_set('Europe/London');
    $currentPage = $_SERVER['PHP_SELF'];
    $currentTimestamp = strtotime(date('Y-m-d H:i:s'));
    //Define your startTime here in {Year-Month-Day Hour:Minute:Second} format
    $startTime = '2012-04-26 21:57:00';
    //Define your endTime here in {Year-Month-Day Hour:Minute:Second} format.
    $endTime = '2012-04-27 22:00:00';
    $startTimestamp = strtotime($startTime);
    $endTimestamp = strtotime($endTime);
    $numOfSecondsToReload = $startTimestamp - $currentTimestamp;
    if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
    ?>
    <a href="*broadcastlink*">LIVE NOW</a>
    <?php else: ?>
    <p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
    <?php header( "refresh:$numOfSecondsToReload;$currentPage"); ?>
    <?php endif; ?>
    

    Update2: to reload specific div content. here is the code you can use.

    <div id="live">
        <?php
        ob_start();
        date_default_timezone_set('Europe/London');
        $currentPage = $_SERVER['PHP_SELF'];
        $currentTimestamp = strtotime(date('Y-m-d H:i:s'));
        $startTime = '2012-04-27 12:42:20';
        $endTime = '2012-04-28 22:00:00';
        $startTimestamp = strtotime($startTime);
        $endTimestamp = strtotime($endTime);
        $numOfSecondsToReload = $startTimestamp - $currentTimestamp;
        if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
        ?>
        <a href="*broadcastlink*">LIVE NOW</a>
        <?php else: ?>
        <p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
        <?php endif; ob_end_flush(); ?>
        <div id="timeremaining" hidden><?php echo $numOfSecondsToReload * 1000; ?></div>
    </div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        var numOfMiliSecondsToReload = $('#timeremaining').html();
        var timeToReload = (numOfMiliSecondsToReload >= 0) ? numOfMiliSecondsToReload : 86400000;
        $(function() {
            $(function() { setTimeout( reloadDiv, timeToReload ); });
            function reloadDiv() { $('#live').load(location.href);}
        });
    </script>
    

    hope this helps you.