Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Timestamp Javascript coding help required

    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

    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...
    Last edited by AndyP; 06-06-2005 at 11:22 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    A Google search holds the extra line you need.

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

    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);
      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

  • #3
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this

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

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    I should have removed the alert line:
    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);
       // 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>
    Last edited by rwedge; 06-07-2005 at 08:41 PM.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    I should have removed the alert line:
    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);
       // 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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •