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