0

I want to reload/refresh a (WordPress) webpage at specific times during a day. So no exact/specific intervals that you can do with <?php header("Refresh:60"); ?> or the javascript function setInterval().

For example I want to reload the page only at these times: 08:30 09:00 09:15 14:20

I want to solve this with PHP or jQuery/JavaScript.

Example of my code (WordPress):

<?php
    // Get Start time and End time from Wordpress using Custom Fields
    $sessionTimeStart = get_field('session_time_start');
    $sessionTimeEnd = get_field('session_time_end');
    $sessionTitle = get_the_title();
    $sessionContent = get_the_content();

    // Current time 
    $currentTime = current_time('H:i');

    if($currentTime > $sessionTimeStart && $currentTime < $sessionTimeEnd) {

        // Prints info about the specific session
        echo $sessionTitle;
        echo $sessionContent;
    }
?>  

Any suggestions?

AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
T77
  • 11
  • 1
  • 5
  • You'll have to do this with javascript I think, not PHP, because PHP is server side and will only execute on page load. I made a website somewhat recently that does sort of does this except instead of refreshing the page it plays an alarm at specific times. Basically it's just a javascript timer that checks every minute what time it is, and if it's a specific time listed in my database it plays an alarm. You could do this similarly with a page refresh instead of a sound bit. – GrumpyCrouton Sep 07 '17 at 15:05
  • 3
    Possible duplicate of [How to refresh page on specific day at specific time in specific time zone?](https://stackoverflow.com/questions/8780529/how-to-refresh-page-on-specific-day-at-specific-time-in-specific-time-zone) or [How to automatically reload a web page at a certain time?](https://stackoverflow.com/a/1217945/5827005) – GrumpyCrouton Sep 07 '17 at 15:09

1 Answers1

1

Based on another answer on SO (How to automatically reload a web page at a certain time?), you could write a function like so:

function refreshAt(hours, minutes, seconds) 
{
    var now = new Date();
    var then = new Date();

    if ( now.getHours() > hours || (now.getHours() == hours && now.getMinutes() > minutes) || now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds ) 
    {
        then.setDate(now.getDate() + 1);
    }
    then.setHours(hours);
    then.setMinutes(minutes);
    then.setSeconds(seconds);

    var timeout = (then.getTime() - now.getTime());
    setTimeout(function() { window.location.reload(true); }, timeout);
}

and then run it:

refreshAt(18,45,0); //Will refresh the page at 18:45pm
Stuart
  • 6,630
  • 2
  • 24
  • 40
  • 5
    It seems sort of unfair that you copied an answer exactly from another answer and posted it as your own while just mentioning it's from another answer but not actually linking to it. Seems like if an answer is almost exactly the same it should just be marked as a duplicate, yes? For future readers, the original answer is posted [here](https://stackoverflow.com/a/1217945/5827005) – GrumpyCrouton Sep 07 '17 at 15:09
  • 1
    Very true @GrumpyCrouton, i have updated the answer. I'll consider myself told, link to answers in future. – Stuart Sep 07 '17 at 15:26