Page 1 of 1

Count Up Script

Posted: Fri Jul 30, 2004 12:20 am
by curseofthe8ball
I have the following code and it works fine for displaying the total amount of days + hours + minutes and seconds from a past date but I'm looking to extend the script to display the number of years, months (in 1 to 11 format), days (in less then year format). Can anyone help me?

Code: Select all

<script language="JavaScript1.2">

function setcountup(theyear,themonth,theday)&#123;
yr=theyear;mo=themonth;da=theday
&#125;

setcountup(1997,1,31)

var displaymessage="have passed since the debut of our site!"

var countupwidth='95%'
var countupheight='20px' //applicable only in NS4
var countupbgcolor='lightyellow'
var opentags='<font face="Verdana"><small>'
var closetags='</small></font>'

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

function start_countup()&#123;
if (document.layers)
document.countupnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?document.getElementById("countupie") : countupie
countup()
&#125;

if (document.all||document.getElementById)
document.write('<span id="countupie" style="width:'+countupwidth+'; background-color:'+countupbgcolor+'"></span>')

window.onload=start_countup


function countup()&#123;
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray&#1111;todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
paststring=montharray&#1111;mo-1]+" "+da+", "+yr
dd=Date.parse(todaystring)-Date.parse(paststring)
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 (document.layers)&#123;
document.countupnsmain.document.countupnssub.document.write(opentags+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds "+displaymessage+closetags)
document.countupnsmain.document.countupnssub.document.close()
&#125;
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds "+displaymessage+closetags

setTimeout("countup()",1000)
&#125;
</script>
To be more exact, I'm looking to count up from the date of January 31st, 1997. I need the format to show something similar to this:

"7 years, 5 months, 29 days, 1 hours, 19 minutes, and 13 seconds have passed since the debut of our site!"

Posted: Fri Jul 30, 2004 12:08 pm
by curseofthe8ball
anyone?

Posted: Fri Jul 30, 2004 2:05 pm
by pickle
I know this is client-side, but is there any reason why it can't be computed server-side and just dumped to the page? If it can be done server side I have a feeling it could be done much easier.

Also, have leap years been considered?

Posted: Fri Jul 30, 2004 2:11 pm
by feyd
the only thing that wouldn't allow server-side is the interval he's using.. so it's updating on a per second basis.. :?

Posted: Fri Jul 30, 2004 2:16 pm
by pickle
Ah, good point. Didn't even see that.

Posted: Fri Jul 30, 2004 2:19 pm
by curseofthe8ball
I'm not locked into using the above code. If someone knows of a better code, by all means paste it here and let me take a look.

Posted: Fri Jul 30, 2004 2:40 pm
by pickle
Well, here's some that gives you that count on every page load, rather than real-time every second:

Code: Select all

<?

elapsed(1997,1,31);


function elapsed($p_year,$p_month,$p_day)
{
  $start_time = mktime(0,0,0,$p_month,$p_day,$p_year);
  $elapsed_time = time() - $start_time;

  $minutes = 60;
  $hours = $minutes * 60;
  $days = $hours * 24;
  $months = $days * 30;
  $years = $days * 365;

  list($elapsed_y,$remainder_y) = crunch($elapsed_time,$years);
  list($elapsed_m,$remainder_m) = crunch($remainder_y,$months);
  list($elapsed_d,$remainder_d) = crunch($remainder_m,$days);
  list($elapsed_h,$remainder_h) = crunch($remainder_d,$hours);
  list($elapsed_min,$elapsed_s) = crunch($remainder_h,$minutes);

  echo "A total of $elapsed_y years, $elapsed_m months, $elapsed_d days, $elapsed_h hours, $elapsed_min minutes and $elapsed_s seconds have passed since the debut of the site.";
}

function crunch($timespan,$units)
{
  $elapsed = floor($timespan/$units);
  $remainder = floor($timespan%$units);

  return(array($elapsed,$remainder));
}

?>
It can be shrunk down a little, but I wrote it out long to make it easier to understand.

Also, leap years are not taken into consideration.

Posted: Fri Jul 30, 2004 3:09 pm
by curseofthe8ball
Thanks for the code. It does work nicely but I'm hoping someone can help me build upon it.

For instance, if someone could help me make the code consider leap years, that would be a HUGE help.

Secondly, I see in the code "$months = $days * 30;" but what about the months that have more or less then 30 days?

Can anyone help a novice (me!) here?

Posted: Fri Jul 30, 2004 4:13 pm
by pickle
curseofthe8ball wrote: For instance, if someone could help me make the code consider leap years, that would be a HUGE help.
Well, you could take the start year, and walk up through each year, checking if it's a leap year. If you come to one, add a days worth of seconds to the count.
curseofthe8ball wrote: Secondly, I see in the code "$months = $days * 30;" but what about the months that have more or less then 30 days?
Good question. February would need to be considered too. Again, you might have to walk through the months 1 month at a time and see how many days there are. Not too sure about this one though.