/    Sign up×
Community /Pin to ProfileBookmark

Countdown Timer from Server Time Seed – NaN error

I have worked on this for almost a month to no avail, so I must finally ask for help.

I wish to create a countdown timer that uses the time from the server (not the local machine) eg, current_time = <?php server_time_as_the_seed; ?> and have the countdown display the Days, Hours, Minutes, Seconds in a continuing fashion. eg, 58, 57, 56… you get the idea.

I have tried modifying numerous javascript countdown clocks with almost identical disappointing results. I can pass the server’s time in php (time()) and the scripts will display the correct time until the event, but WILL NOT continue to countdown.

I have tried things like adding Javascript’s Date() to PHP time(), then subtracting it (or portions of it) back out again to sort of jumpstart the countdown process. While the countdown will begin counting, the dates are way off. Then when I do get close I get NaN results (even if I manually set types to Intergers). eg. (JS_Date + php_time() – JS_Date) a very rough example.

I have tried setting the time both inside and outside functions, all ending in the same displaying time and no counting down, 12, 11, 10….

Summary:
Apache Server (10:00:00am) >php4.2 (date or time function seed 10:00:00am)> Javascript 1:00:00 until 11:00:00 am on the server (59:59 59:58 59:57 …) remaining.

Again, I must be missing something incredibly simple here, so any suggestions or examples will be greatfully received.

As for an example of countdown code, I have downloaded at least 25 different examples posted by others as a base to work from, none of which have I been able to make work for me. If someone can simply post a bit of something that will work, I am confident that I will be able to incorporate it into my desired script. If however you do wish to see the script that currently seems to be the one I am leaning toward using, I will post it (with credits) if requested.

Again, Thank you.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 14.2004 — you can not use JavaScript,since it resides on the client(web browser),for it to get a continous feed(time feed in ur case) it will require a dedicated connection to the server(which using JS is not possible).

Here are coupe of options though.

create a js file,and re-write it every few seconds (extremely resource consuming?) and read some variables in the page using JS(u can use setTimeout()).

Second you may get away with using some thread programming using Java Applets as well.
Copy linkTweet thisAlerts:
@hotmonkeyauthorJun 14.2004 — Is it not possible to use the server's time as a seed for the local machine to work from?

server time + local machine date will give me a date way into the future so the two numbers do seem to add together, or if I start subtracting one from another I seem to get a working number, only when I start the second round of math to bring it back to the server's time do problems start to arise with NaN errors.

eg, (server + local) or (server - local) counter counts though wrong

(server + local - local) or (server - local + local) NaN

again rough concept examples


Thank you.
Copy linkTweet thisAlerts:
@hotmonkeyauthorJun 15.2004 — It's funny how when you get lots of "it can't be done" on one side and "impossible is unexceptable" on the other, somehow it just works out...

If someone has a moment to review this for logic flaws I would welcome a more efficient version, but it now seems to be working for me (after a month of fiddling with ideas). I think this might be helpful to folks who would like an idea of how to use the server time as a source for a Javascript countdown vs the local machine's time, so that everyone has the same end or finish time regardless where they are or their machine's current time.

General Variables being setup:

var currentDateServer = <?php echo (time() * 1000);?> ;

var currentDateLocal = new Date();

var currentDateDifference = currentDateLocal.getTime()-currentDateServer;

var currentDateOffset = Number(currentDateDifference);

Part of the function that does the countdown and display:

function doCountdown() {

for (i = 0; i < myCountdown.length; i++) {

if (!myCountdown[i].expired) {

var currentDateCounting = new Date();

var currentDate = currentDateCounting-currentDateOffset; //Subtract the difference to get back to Server time

var eventDate = myCountdown[i].eventDate;

var timeLeft = new Date();

timeLeft = eventDate - currentDate;

....



I have simply taken the difference in times and turned it into a number then subtracted it from the local machine to get back to the Server's time, not millisecond accurate but it seems to be working, updating now! 59, 58, 57....



If someone can see a better way to do this, I think that I and others would greatly appreciate it.
×

Success!

Help @hotmonkey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...