Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a solution that uses <a href="http://keith-wood.name/countdown.html" rel="nofollow">a countdown plugin for jQuery</a>. Download the <a href="http://keith-wood.name/zip/jquery.countdown.package-1.5.9.zip" rel="nofollow">jQuery Countdown plugin</a>, and place <code>jquery.countdown.css</code> and <code>jquery.countdown.min.js</code> in the same folder as your file (or elsewhere, and adjust the paths in the code below). In the head section of your document include this:</p> <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="jquery.countdown.min.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt;@import "jquery.countdown.css";&lt;/style&gt; </code></pre> <p>Somewhere in the PHP code, before anything is outputted, run this to set when the time will be:</p> <pre><code>if (!isset($_COOKIES['endtime'])) { $endtime = time() + 28800000; setcookie('endtime', $endtime); } else { $endtime = $_COOKIE['endtime']; } </code></pre> <p>Then, where you want the timestamp to appear, output this HTML:</p> <pre><code>&lt;span class="countdown hasCountdown" id="myCountdown"&gt;&lt;/span&gt; </code></pre> <p>And somewhere after it, output this JavaScript:</p> <pre><code>var targetDate = new Date(&lt;?php echo ($endtime ); ?&gt;); $('#myCountdown').countdown({until: targetDate}); </code></pre> <hr> <p><strong>EDIT</strong>: Alright, here are some improvement on my previous answer to include pausing when the user leaves the page. I'm also switching the post over to community wiki so anyone is welcome to edit and improve it.</p> <p>In order to track when the user was last on the page, you need to keep a cookie that you update every 5 seconds or so with the current time. Here's a bit of JavaScript that does just that:</p> <pre><code>function setCookie(c_name, value, exdays) { var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie = c_name + "=" + c_value; } function updateCookie() { var curTime = new Date(); setCookie('lastvisit', curTime.getTime(), 30); setTimeout("updateCookie()", 5000); } </code></pre> <p>You need to run <code>updateCookie()</code> once and then it will keep running every five seconds. To have it run for the first time as soon as the page has finished on loading, add an onLoad to your body tag, like this:</p> <pre><code>&lt;body onLoad="updateCookie()"&gt; </code></pre> <p>Your PHP code will then look like this instead of the one above:</p> <pre><code>if (!isset($_COOKIES['lastvisit']) || !isset($_COOKIES['endtime'])) { $endtime = (time()*1000) + 28800000; setcookie('endtime', $endtime, time() + 2592000); } else { $prevendtime = $_COOKIE['endtime']; $lastvisit = $_COOKIE['lastvisit']; $timeleft = $prevendtime - $lastvisit; $endtime = (time()*1000) + $timeleft } </code></pre> <p>All the cookies are set to expire after 30 days, so if your user stays away for more than 30 days they will reset.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload