Crisman
Crisman

Reputation: 418

Countdown Clock

I would like to create a countdown clock that when you show up on the page it starts to countdown from a pre-defined number, say 8 hours. If you leave the page, say after 2 hours, I would like it to stop counting down. If you return to the page I want it to resume counting down, now with only 6 hours left.

Is this possible using javascript client side, and php server side?

If I wanted to create multiple user generated countdowns can I use mySQL to store the different variables?

Obviously I have a lot to work out here, I don't want someone to write me the whole code. I am looking for a starting point, maybe some tutorials or advice on which way to start perusing. I am very much a novice at js, php, mysql but can work with them.

Upvotes: 1

Views: 2305

Answers (2)

Madara's Ghost
Madara's Ghost

Reputation: 175088

Give the user a unique ID via a cookie, using uniqid(), and then save the unique id and the time of the countdown on the server side (in a database or a file), then, once the user enters and his browser dilvers the cookie, figure out the unique ID, and give him the time needed for the countdown.

As for the countdown itself, there are plenty of plugins such as this excellent plugin given by the answerer above me. Pass the date to JavaScript's Date object via PHP, and use the plugin.

Upvotes: 2

Edoardo Pirovano
Edoardo Pirovano

Reputation: 8334

Here's a solution that uses a countdown plugin for jQuery. Download the jQuery Countdown plugin, and place jquery.countdown.css and jquery.countdown.min.js 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:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="jquery.countdown.min.js"></script>
<style type="text/css">@import "jquery.countdown.css";</style>

Somewhere in the PHP code, before anything is outputted, run this to set when the time will be:

if (!isset($_COOKIES['endtime'])) {
    $endtime = time() + 28800000;
    setcookie('endtime', $endtime);
} else {
    $endtime = $_COOKIE['endtime'];
}

Then, where you want the timestamp to appear, output this HTML:

<span class="countdown hasCountdown" id="myCountdown"></span>

And somewhere after it, output this JavaScript:

var targetDate = new Date(<?php echo ($endtime ); ?>); 
$('#myCountdown').countdown({until: targetDate});

EDIT: 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.

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:

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);
}

You need to run updateCookie() 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:

<body onLoad="updateCookie()">

Your PHP code will then look like this instead of the one above:

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
}

All the cookies are set to expire after 30 days, so if your user stays away for more than 30 days they will reset.

Upvotes: 3

Related Questions