/    Sign up×
Community /Pin to ProfileBookmark

Sever Countdown Timer

Hi,

I’ve got a javascript countdown timer that counts down to a time stored in a mysql database. Im passing the time from the database to the script using php. It works fine.

The problem is – I cant get it to accept variables from the current server time using php rather than the local time.

[url]http://asacarter.plus.com/marketplace/view.php?id=1[/url]

Here’s the working code:-

[code]
[head]
<? include(“common/connect.php”);

$end_query = “SELECT UNIX_TIMESTAMP(auction_end) FROM item WHERE id = $id”;
$end_result = mysql_query($end_query, $mysql_access);
@list($date) = mysql_fetch_array($end_result);

$year = date(Y, $date);
$month = date(n, $date);
$day = date(j, $date);
$hour = date(G, $date);
$minute = date(i, $date);
$second = date(s, $date);
echo “<script language=’javascript’>”;
echo “Year=”.$year.”n”;
echo “Month=”.$month.”n”;
echo “Day=”.$day.”n”;
echo “Hour=”.$hour.”n”;
echo “Minute=”.$minute.”n”;
echo “Second=”.$second.”n”;
echo “</script>”;
?>

<script type=”text/javascript” language=”JavaScript”>

function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}

setcountdown(Year,Month,Day,Hour,Minute,Second)

var montharray=new Array(“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”)
var crosscount=”

function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todayd=today.getDate()
var todaym=today.getMonth()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+” “+todayd+”, “+todayy+” “+todayh+”:”+todaymin+”:”+todaysec
futurestring=montharray[mo-1]+” “+da+”, “+yr+” “+hr+”:”+min+”:”+sec
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

if (dday!=1 && dday!=0) {
var SufDays=” days, “}
else if (dday<=0) {
var SufDays=””
var dday=””}
else {var SufDays=” day, “}

if (dhour!=1 && dhour!=0) {
var SufHour=” hours, “}
else if (dhour<=0) {
var SufHour=””
var dhour=””}
else {var SufHour=” hour, “}

if (dmin!=1 && dmin!=0) {
var SufMin=” minutes and “}
else if (dmin<=0) {
var SufMin=” and ”
var dmin=””}
else {var SufMin=” minute and “}

if (dsec!=1) {
var SufSecs=” seconds”}
else {var SufSecs=” second”}

if (dday<=-1){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(“Auction has ended”)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=”Auction has ended”
return
}
else{
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(dday + SufDays + dhour + SufHour + dmin + SufMin + dsec + SufSecs)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=dday + SufDays + dhour + SufHour + dmin + SufMin + dsec + SufSecs
}
setTimeout(“countdown()”,1000)
}
</script>
[head]

<body onload=”start_countdown()”;>
<script type=”text/javascript” language=’javascript’>
function start_countdown(){
if (document.layers)
document.countdownnsmain.visibility=”show”
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?document.getElementById(“countdownie”) : countdownie
countdown()
}
if (document.all||document.getElementById)
document.write(‘<span id=”countdownie” width: 300px; height: 12px;”></span>’)
</script>
[/code]

Any idea’s on how I can get the server time rather than the local time?

Thanks,

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@asa_carterauthorOct 09.2003 — I have found this code by searching the forums:-

<i>
</i>servdate = new Date(&lt;?PHP echo time()*1000; ?&gt; );
servtime = servdate.getTime();
var today = new Date(servtime);


It produces the correct time but the timer stops updating automatically.

Does anyone know what is wrong?

Thanks,
Copy linkTweet thisAlerts:
@DimitriOct 09.2003 — what do you mean the "timer stops updating automatically"?

I ran your code. It counts down and then shows "Auction has ended."

So, I don't get what you're asking for (sorry)... what else do you want it to do?

D
Copy linkTweet thisAlerts:
@asa_carterauthorOct 09.2003 — Hi.

Thats strange.

Here's the current data from the link I gave:-

Time Left 25 days, 21 minutes and 8 seconds

Started Thu 9th Oct 2003, 7:36 pm GMT

Ends Mon 3rd Nov 2003, 8:22 pm GMT

The auction expires on the 3rd november, so it shouldn't display that message until that date.

In my browser it doesn't count down at all now that i've added this code:

<i>
</i>servdate = new Date(&lt;?PHP echo time()*1000; ?&gt; );
servtime = servdate.getTime();
var today = new Date(servtime);
Copy linkTweet thisAlerts:
@DimitriOct 09.2003 — Sorry, I just realized that your code expects the date() object to be contantly refreshed (once a second according to your js timer).

You want this page to somehow get the current SERVER time every 1 second?

Well, the way your current script is written, the only way you can do that is by refreshing the page... once a second.

Sorry.

Dimitri
Copy linkTweet thisAlerts:
@asa_carterauthorOct 09.2003 — Hi,

How would I go about changing the script to constantly refresh the server time without refreshing the whole page or using meta-refresh?
Copy linkTweet thisAlerts:
@DimitriOct 09.2003 — What I recommend is that you rewrite your script so that it takes the server time ONCE then uses the user computer's time to increment the time.

So, for example...

the paged loads and the "today" JS variable is set by php.

every one second, you instantiate a new js date object and subtract the date() object [which the php originally created when the page loaded] from the new js date object(), to find out how many milliseconds have passed.

then use that value to estimate what the current server time is (by adding these milliseconds to a new date() object to create "today" date() object.)

Are you following me?

It won't be perfect, but it'll be close enough to work just fine, expecially if you refresh the page every 20 minutes to make sure it's synced up with the server time.

Dimitri
Copy linkTweet thisAlerts:
@asa_carterauthorOct 09.2003 — Thanks. Thats a good idea but it could it would provide a security loophole where a user could load the page, change their system clock, and bid on item that has closed. Thus being able to get the final bid and winning the item after the auction has closed.

...Unless I could check the time that the user presses the bid button does not exceed the current server time?
Copy linkTweet thisAlerts:
@DimitriOct 10.2003 — Yes. The time shown on the user's screen is only there as a convenience to the user.

The actual time left is determined by the server.

When a user submits their bid to you, it doesn't matter what their computer's time says. You already know the exact moment you received the bid from the user; that's the time you need to go by (the server's clock).

Cheers.

D
Copy linkTweet thisAlerts:
@asa_carterauthorOct 12.2003 — Hi,

Could you give me some help with the js code to subtract the server date object from the current date object to get the number of milliseconds and add it to the today date object?

Thanks,
×

Success!

Help @asa_carter 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.18,
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,
)...