0

Below is my code to convert time to a fancy style for eg. 1 min ago, 9 hours ago etc using moment.js.

 <script>
                $(document).ready(function() {
            var out = "";   

            var diffDays =  moment.utc("<?php echo $row["created_date"]; ?>", "YYYY-MM-DD hh:mm").fromNow();

            out += "<span class='glyphicon glyphicon-time hours'></span> "+ diffDays;

$("#divContent<?php echo $row["postid_userid"]; ?>").html(out);  
 });
 </script>   

The code is working fine.

I have an ajax load more option on the same page to load more posts when scrolling.

My main problem is that, the moment js I am using stopped working after ajax call. Is this the problem because the code is not getting moment.js on my ajax page get_results.php ?

This is my ajax function (which is on the same page and calls the other page to load more posts)

 <script type="text/javascript">

  $(window).scroll(function(){
  if ($(window).scrollTop() == $(document).height() - $(window).height()){

 var val = document.getElementById("id").value;

document.getElementById("id").value = Number(val)+5;

  $.ajax({           
  type: 'post',
  url: 'get_results.php',
  data: { 
   getresult:val
  },

  success: function (response) {
      loading = false;
      if(response.trim().length == 0){
            //notify user if nothing to load
            $('.loading-info').html("No more posts!");
            document.getElementById("loading-info").style.display = "block";
            return;
        }
        else
        {

             var content = document.getElementById("all_rows");
             content.innerHTML = content.innerHTML+response;

        }
        $('.loading-info').hide(); //hide loading animation once data is received

  }
  });

  }
});

And this is the get_results.php page:

<?php session_start();
    $user_id=$_SESSION['user_id'];
    include "mydbfile.php"; 

   if(isset($_POST['getresult']))
   {
     $no = $_POST['getresult'];
            ?>

                <?php       
                 $select =$conn->prepare("select * from ----- ");


   $select->execute();                  
   while($row =$select->fetch()) 
 {  

?> 
  //design to display more posts

 <?php 
    } 

         } exit(); ?>    
user3657517
  • 35
  • 1
  • 9
  • when you say "stops working", what do you mean exactly? do you get a specific error in the console? – ADyson Aug 16 '16 at 08:57
  • It is not displaying results for eg. 15 hours ago. It returns a blank div without any result. @ADyson – user3657517 Aug 16 '16 at 09:08
  • Do you mean that it doesn't format the dates of the posts loaded via ajax? If so, well, you haven't posted all the code in get_results.php that creates the posts, but I would guess that you haven't applied any formatting to those dates. The momentjs you've got in the other page only applies to stuff that's output from that page, not from get_results. Incidentally, you don't need momentjs for your use case - you're applying it to a date from the server side so you could just use PHP's date functions to do the same job :-) – ADyson Aug 16 '16 at 09:47
  • Yes it does not format the time for the post that are coming from ajax. I have used the same js script to format the date on get_results.php as well which is on my default page. Moment need moment.js file to work, is this the case that the js script i have added in get_results.php page, is not abe to access moment.js which i have included in head tag on my default page? @ADyson – user3657517 Aug 16 '16 at 09:59
  • the date formatting script as you've posted is only handling data that's rendered to the client when the main page is rendered. It runs immediately when the page loads, and runs once using the value of "date_created" which you inject into it from a PHP variable. There's no way that get_results.php can make use of this. But I'll repeat, you don't need moment for this. If you're creating the HTML server-side in get_results.php then just use PHP's date function: http://php.net/manual/en/function.date.php – ADyson Aug 16 '16 at 10:12
  • Will it give me time difference like moment does? @ADyson – user3657517 Aug 16 '16 at 11:36
  • with a tiny bit of effort, yes. See http://stackoverflow.com/questions/10696654/get-total-time-difference-between-two-dates-using-php for instance – ADyson Aug 16 '16 at 11:48
  • Tanks a lot for you time. Will try this for sure. – user3657517 Aug 16 '16 at 12:08

0 Answers0