Php 如何使帖子日期同步

Php 如何使帖子日期同步,php,javascript,ajax,Php,Javascript,Ajax,假设我在一个页面上有15篇或更多的帖子。while循环中的代码如下所示 <div class="post" id="<?php echo $postID?>"> <h1><?php echo $title;?></h1> <div class="postmeta"> posted by <?php echo $author;?> , <?php echo timeAgo($postD

假设我在一个页面上有15篇或更多的帖子。while循环中的代码如下所示

 <div class="post" id="<?php echo $postID?>">

    <h1><?php echo $title;?></h1>

    <div class="postmeta"> 
posted by <?php echo $author;?> , <?php echo timeAgo($postDate);?></div>
    </div>

我会使用像这里这样的数据属性将unix时间戳添加到div中的一个范围中,该范围将包含“time ago”字符串。
然后将timeAgo逻辑移植到javascript,并使用setInterval每分钟更新一次

不需要为这么简单的事情使用AJAX,但是请注意下面的示例使用了

为了完整起见,下面是代码:

<div class="postmeta"> 
    posted by Foo , <span class="timestamp" data-timestamp="1329573254200"></span> ago
</div>​

<script>
updateTimestamps();
setInterval(updateTimestamps, 60000);

function updateTimestamps(){
    $(".timestamp").each(function(i){
        var timestamp = $(this).data('timestamp');

        // Put your time ago logic here
        var timeAgo = timestamp;
        $(this).text(timeAgo);
    });
}
</script>

由Foo,ago发布
​
updateTimestamps();
setInterval(updateTimestamps,60000);
函数updateTimestamps(){
$(“.timestamp”)。每个(函数(i){
var timestamp=$(this.data('timestamp');
//把你的时间逻辑放在这里
var timeAgo=时间戳;
$(此).text(时间);
});
}

使用jquery是一种很好的方式。 这是一个例子:

      <script src=jquery.js"></script>
         <script>

     <script>

    var auto_refresh = setInterval(
    function()
    {
// here put a url to a script to calculate time posted ago
    $('#someid').load('someurl');
    }, 60000);
    </script>
   <div class="post" id="<?php echo $postID?>">

    <h1><?php echo $title;?></h1>

    <div class="postmeta" id='someid'> 
posted by <?php echo $author;?> , <?php echo timeAgo($postDate);?></div>
    </div>


我希望这能帮助您

您应该在服务器上进行排序,它应该与JavaScript无关。一个页面上也有多个h1?是的,这只是一个例子。我在数据库中有post-unix时间戳这个答案是利用jquery框架。你至少应该说明你正在使用它。考虑把确切的时间写在跨度内,这样没有JS的人也会知道时间。
      <script src=jquery.js"></script>
         <script>

     <script>

    var auto_refresh = setInterval(
    function()
    {
// here put a url to a script to calculate time posted ago
    $('#someid').load('someurl');
    }, 60000);
    </script>
   <div class="post" id="<?php echo $postID?>">

    <h1><?php echo $title;?></h1>

    <div class="postmeta" id='someid'> 
posted by <?php echo $author;?> , <?php echo timeAgo($postDate);?></div>
    </div>
.....  continue
$.ajax({
 // here put a url to a script to calculate time posted ago 
  url: 'url',
  success: function(data) {
    $('#someid').html(data);

  }
}); 
......