php forum
php mysql forum
php mysql smarty
 
Topic Options
#109986 - 02/10/06 05:30 AM Forum Age
Raziel Offline
Junior Member

Registered: 01/22/06
Posts: 11

   Forum Age to Del.icio.us Add to del.icio.us
  Digg Forum Age Digg it
Whats wrong with this code?

var startDate = ['08','02','2006'];

// Forum Age
var sta = new Date();
sta.setDate(startDate[0]);
sta.setMonth(startDate[1]-1);
sta.setYear(startDate[2]);
var cur = new Date();
var dif = cur - sta;
var totalMonth = 0;
var totalYear = 0;
var totalDay = Math.floor(dif/(60 * 60 * 24 * 1000)) + 1;
while(totalDay > 365) {
totalDay -= 365;
totalYear++;
}
while(totalDay > 30) {
totalDay -= 30;
totalMonth++;
}
var age = totalDay+' days';
if(totalMonth > 0)
age += ', '+totalMonth+' months';
if(totalYear > 0)
age += ', '+totalYear+' year';
age += '.';

Top
#109987 - 02/14/06 12:00 AM Re: Forum Age
Brett Offline
Moderator

Registered: 05/04/01
Posts: 992
Loc: Twinsburg, Ohio
The plus 1 on the totalday variable seems to throwing things off by one day. Fundamentally the month/year counts will be off due to the fact that (leap) years and every other month is not 30 days. Correcting this by taking into account such variations into months will be a hefty amount of javascript.


More or less out of boredom I cleaned up the code taking out the while loops
Code:
<script type="text/javascript">
<!--
var startDate = ['08','02','2005']; 

// Forum Age
var sta = new Date(startDate[2], startDate[1] - 1, startDate[0]);
var dif = (new Date() - sta) / 86400000; // milliseconds in day
var age, display = new Array(0, 0, 0);

// Find Years
if(dif >= 365) {
	display[0] = Math.floor(dif / 365);
	dif -= (display[0] * 365); // take days off total
}

// Find Months
if(dif >= 30) {
	display[1] = Math.floor(dif / 30);
	dif -= (display[1] * 30); // take days off total
}

// Find Days
display[2] = Math.floor(dif);

document.writeln(display[2] + " days, " + display[1] + " months, " + display[0] + " years");
-->
</script>

Top
#109988 - 03/09/06 07:19 AM Re: Forum Age
Raziel Offline
Junior Member

Registered: 01/22/06
Posts: 11
Thanks Brett smile

Top


Who's Online
3 Registered (Gizmo, Zarzal, blaaskaak), 29 Guests and 6 Spiders online.
Key: Admin, Global Mod, Mod
Shout Box

Latest Posts
Blogs, love em or hate em?
by AllenAyres
10/07/08 02:05 PM
What do you use to edit the files
by Ian_W
10/05/08 03:33 PM
BeyondCompare v3.00
by Ian_W
10/05/08 03:32 PM
Glossy Black Theme with Image Reflection
by Gizmo
10/05/08 02:17 PM
ShareThis
by Gizmo
09/28/08 05:06 AM
[7.3] Viewing MySQL logfiles made easier
by AllenAyres
09/27/08 09:57 PM
Looking for a simple upload script
by Murphdog
09/26/08 08:45 PM
New Mods
[7.3] Viewing MySQL logfiles made easier
by blaaskaak
09/24/08 05:39 PM
[7.3] Language file checker
by blaaskaak
09/09/08 12:56 AM
[7.3.1] add search to showmembers page
by blaaskaak
09/07/08 04:50 AM
Multiple Identity Detector
by
12/30/06 06:39 PM
Newest Members
pisa666, ghengis317, NitroX, Dogan, EliYah-
13345 Registered Users
Top Posters Last 30 Days
AllenAyres 13
blaaskaak 9
tackaberry 7
FREAK1 7
Mike L_dup1 4
Chris Bale 4
Ian_W 4

 

 

 
fusionbb message board php hacks