Title: Timer on Posts
Last modified: February 27, 2017

---

# Timer on Posts

 *  [eVersatile](https://wordpress.org/support/users/eversatile/)
 * (@eversatile)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/)
 * Hello,
    I am curious as to if anyone has any idea or information on how I would
   be able to accomplish adding a timer to posts. More in-depth; I am working on
   creating a blog that allows others to participate in posting articles. When a
   post is in draft status a timer starts counting down, showing the author they
   have that much time to publish the post. Any idea if or how this would be possible?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8854553)
 * It’s certainly possible to some degree. Unless you can find an existing plugin,
   it’ll take some custom coding. I say “to some degree” because the timing may 
   not be very accurate. If it’s a countdown to the second like an auction, that
   could be tricky. If it’s post before this date, give or take a few dozen minutes
   around midnight, and your site is visited by bots or people somewhat regularly,
   then yes.
 * You can [schedule events](https://developer.wordpress.org/reference/functions/wp_schedule_event/)
   in WP. One could check for posts that have just expired and flip some data, like
   change status from draft to publish. WP events can run hourly, which is as frequent
   as supported. Exactly when the event runs requires a site visit after the designated
   time to start the process. There’s a couple alternatives for infrequently visited
   sites.
 *  Thread Starter [eVersatile](https://wordpress.org/support/users/eversatile/)
 * (@eversatile)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8866233)
 * Thank you for the information.
    The timer does not have to be 100% accurate. 
   Just a ‘post before this date’. I have already created an event to change the
   post from draft to ‘pending’ if they do not ‘publish’ by the end of that date.
   Though, I have not figured out a way to start a visual timer once the post is
   saved as a draft.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8867516)
 * Get the deadline from where ever it’s stored. If it’s not a timestamp, convert
   it to one with strtotime(). (FWIW it’s always a good idea to store dates as timestamps
   IMO). The current timestamp is returned with now(). Figure the difference, which
   will be time to deadline in seconds. Convert this to whatever format for display.
   date() usually works well for this.
 * The display remains static until the page is reloaded, so avoid a seconds display,
   maybe even minutes. AJAX can be used to keep the display current, but this complicates
   the entire process.
 *  Thread Starter [eVersatile](https://wordpress.org/support/users/eversatile/)
 * (@eversatile)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8875163)
 * I have put together the following code, though it is resetting everytime I reload
   the page.
    Do you know how I could prevent the timer from resetting?
 *     ```
       <?php
       session_start();
       $timestamp = time();
       $diff = 86400;
   
       $hld_diff = $diff;
       if(isset($_SESSION['ts'])) {
       	$slice = ($timestamp - $_SESSION['ts']);	
       	$diff = $diff - $slice;
       }
   
       if(!isset($_SESSION['ts']) || $diff > $hld_diff || $diff < 0) {
       	$diff = $hld_diff;
       	$_SESSION['ts'] = $timestamp;
       }
   
       $diff; //$diff holds seconds less than 3600 (1 hour);
   
       $hours = floor($diff / 3600) . ' : ';
       $diff = $diff % 3600;
       $minutes = floor($diff / 60) . ' : ';
       $diff = $diff % 60;
       $seconds = $diff;
   
       ?>
       <div id="strclock">Clock Here!</div>
       <script type="text/javascript">
        var hour = <?php echo floor($hours); ?>;
        var min = <?php echo floor($minutes); ?>;
        var sec = <?php echo floor($seconds); ?>
   
       function countdown() {
        if(sec <= 0 && min > 0) {
         sec = 59;
         min -= 1;
        }
        else if(min <= 0 && sec <= 0) {
         min = 0;
         sec = 0;
        }
        else {
         sec -= 1;
        }
   
        if(min <= 0 && hour > 0) {
         min = 59;
         hour -= 1;
        }
   
        var pat = /^[0-9]{1}$/;
        sec = (pat.test(sec) == true) ? '0'+sec : sec;
        min = (pat.test(min) == true) ? '0'+min : min;
        hour = (pat.test(hour) == true) ? '0'+hour : hour;
   
        document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec;
        setTimeout("countdown()",1000);
        }
        countdown();
       </script>
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8875450)
 * I don’t see any problems with what you have. After not seeing anything for a 
   while, I copied your code into a PHP page and loaded it. It works fine on my 
   server. Reloading the page does not reset the clock.
 * All I can imagine is there’s an issue with your server’s PHP session implementation.
 *  Thread Starter [eVersatile](https://wordpress.org/support/users/eversatile/)
 * (@eversatile)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8876215)
 * I believe you are most likely correct, but I am having trouble on figuring out
   how to fix it.
    I added `<?php if(!isset($_SESSION)){ session_start(); echo session_id();}?
   >` to check for the session ID when I reload the page. The session ID changes
   every time I reload. How can I keep the session going? Along with a new session
   created when a new post is created? I imagine the best way to go would be to 
   create one session for each post and keep it on there for 30 days….
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8876858)
 * You had it right to start with. You need to call session_start() on every new
   request regardless of $_SESSION having any values or not. By doing this, PHP 
   figures out the internal workings itself, all you need to do is set or use $_SESSION
   after calling session_start(). There’s obviously many other session functions,
   but for the most part there is no need to use them. Typically PHP sessions just
   work.
 * If you are running this code on a hosted server, inquire with your host about
   PHP sessions not working. Use a simple, proven example like the 2 pages at [http://php.net/manual/en/function.session-start.php](http://php.net/manual/en/function.session-start.php)
   as demonstration of the problem. You’ll get undefined variable errors on page
   2 if sessions are not working.
 * If you have this problem on localhost, try it on a hosted server, it may be fine
   there.
 * There’s a few options on where to save values between requests. Cookies, provided
   the user continues to use the same machine and browser. If they are logged in
   you can save values in user meta. You can save data as WP transients, but you’ll
   have to manage the IDs, which brings up the same cookie or user meta issues.
 * If you save the deadline with the post, instead of the “slice” off the countdown,
   it will not matter who accesses the timer, the deadline would be the same for
   all users (if they happened to look, I realize the post only relates to the one
   author). The deadline just needs to be stored somewhere consistent, like post
   meta. Then you don’t need sessions or cookies to store user data, all that matters
   is the time from now to the deadline.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Timer on Posts’ is closed to new replies.

## Tags

 * [post](https://wordpress.org/support/topic-tag/post/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 7 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/timer-on-posts/#post-8876858)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
