PDA

View Full Version : Need Help With My Count-up (server end script)



jkm01
03-10-2010, 11:04 AM
I am trying to create a count up script that works off the server time, this way everyone sees the same count up number regardless of timezone.




</style>

<script type="text/javascript">


function cdlocaltime(container, servermode, offsetMinutes, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
this.localtime=this.serverdate=new Date(servertimestring)
this.targetdate=new Date(targetdate)
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
}

cdlocaltime.prototype.oncountup=function(){} //default action for "oncountup"

cdlocaltime.prototype.start=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
var timediff=(this.targetdate-this.localtime)/1000 //difference btw target date and current date, in seconds
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
var result={days: dayfield, hours:hourfield, minutes:minutefield, seconds:secondfield}
this.oncountup(result)
setTimeout(function(){thisobj.start()}, 1000) //update results every second
}



</script>


<div id="cdcontainer"></div>

<script type="text/javascript">

//SYNTAX: myvariable=new dcountup(past_date_and_time_string, "baseunit")
var update=new dcountup("March 9, 2010 13:45:02", "days")

update.oncountup=function(result){
//result is an object containing the current count up date/time, updated every second
//Available properties: result["days"], result["hours"], result["minutes"], and result["seconds"]
var mycountainer=document.getElementById("cdcontainer")
mycountainer.innerHTML="Geupdate was last detected: <br /><span class='dcountstyle'>"+result['days']+" <sup>days</sup> "+result['hours']+" <sup>hours</sup> "+result['minutes']+" <sup>minutes</sup> "+result['seconds']+" <sup>seconds</sup></span>"
}

</script>

jkm01
03-11-2010, 03:55 PM
Idk what my problem is. I am rather new to javascript, only been playing with it for 4 days now.

what i'm trying to do is have a set time i imputed manually in to the script. Then get the server time, then subtract the server time with the manually imputed time.. to get (x hour, x minute, x seconds) Ago

I dont know if its even pulling the server time. I dont know if its even subtracting.. Can someone please read the script and tell me what i'm doing wrong.