...

View Full Version : Timestamp Javascript coding help required



AndyP
06-06-2005, 11:49 PM
I have a script that I can use to post my news and with the script timestamping each news item that is entered into the database. I then call the item from the database to display it on my site using the timestamp and the following javascript code


<script language="javascript">
function difference(oldtime) {
var nowtime = new Date();
var milliseconds1 = oldtime.getTime();
var milliseconds2 = nowtime.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
if (daysDifference < 1 && hoursDifference < 1)
document.write(' &nbsp;(<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 4)
document.write(' &nbsp;(<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 24)
document.write(' &nbsp;(<span class="lastupdated"><b>' + hoursDifference + ' </b> hours ago)');
else // nothing here
document.write(' ');
}
</script>

... with the intention of providing my readers with an indication of how long ago the news item was posted.

My intention is that depending on when the item was posted, if it was posted under an hour ago the reader will see the message "posted xx minutes ago"; if it was posted between 1 hour and 4 hours ago they will see the message "posted x hours and xx minutes ago"; if it was posted over 4 hours ago and under 24 hours ago they will see the message "posted x hours ago" and beyond that *for items over a day old) they will see nothing.

Here's my problem

The above code works really well for any item posted within 24 hours ago. However, once it gets beyond 24 hours it seems to reset itself and not realise that a day has passed.

For example : if I posted an item at 1.15pm on Monday and it is 2.15pm on Monday the reader will see the message "posted 1 hour ago"; but at 2.15pm on Tuesday they will still see the message "posted 1 hour ago"...

Is there an adjustment to the above code that I can make that will take account of the number of days that have passed because somewhere the code isn't caluclating that properly.

Thanks in advance for any help...

rwedge
06-07-2005, 12:53 AM
A Google (http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=SNYF,SNYF:2004-46,SNYF:en&q=javascript+Math%2Efloor%28difference%2F1000%2F60%2F60%2F24%29%3B) search holds the extra line you need.

In the if/else statement you are not looking for anything over a day old.




<script language="javascript">
function difference(oldtime) {
var nowtime = new Date();
var milliseconds1 = oldtime.getTime();
var milliseconds2 = nowtime.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
alert(daysDifference);

if (daysDifference > 1)document.write('This topic is ' + daysDifference + ' days, ' + hoursDifference + ' hours, ' + minutesDifference + ' minutes, ' + secondsDifference + ' and seconds old.');
else if (daysDifference < 1 && hoursDifference < 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago');
else if (daysDifference < 1 && hoursDifference < 4) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago');
else if (daysDifference < 1 && hoursDifference < 24) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours ago');
else document.write(' ');
}
</script>



/Bob

AndyP
06-07-2005, 08:38 PM
Is this


alert(daysDifference);

the extra line you mean ? If so, then it doesn't work.. it just gives a popup window message :(

rwedge
06-07-2005, 09:33 PM
I should have removed the alert line:
<script language="javascript">
function difference(oldtime) {
var nowtime = new Date();
var milliseconds1 = oldtime.getTime();
var milliseconds2 = nowtime.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
// This next line below looks for entries over a day old
if (daysDifference > 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + daysDifference + '</b> days,<b> ' + hoursDifference + '</b> hours,<b> ' + minutesDifference + '</b> minutes ago</span>');
else if (hoursDifference < 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 4) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 24) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours ago</span>');
else document.write(' ');
}
</script>

rwedge
06-07-2005, 09:37 PM
I should have removed the alert line:
<script language="javascript">
function difference(oldtime) {
var nowtime = new Date();
var milliseconds1 = oldtime.getTime();
var milliseconds2 = nowtime.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
// This next line below looks for entries over a day old
if (daysDifference > 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + daysDifference + '</b> days, <b>' + hoursDifference + '</b> hours,<b> ' + minutesDifference + '</b> minutes ago</span>');
else if (hoursDifference < 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 4) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 24) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours ago</span>');
else document.write(' ');
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum