AndyP
06-06-2005, 10: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(' (<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 4)
document.write(' (<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 24)
document.write(' (<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...
<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(' (<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 4)
document.write(' (<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago)');
else if (daysDifference < 1 && hoursDifference < 24)
document.write(' (<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...