php - 如何显示 "minutes ago"而不是 posted_time 的 UTC 时间?

标签 php jquery css mysql

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 5 年前

我用于调用我的表格的代码以 24 小时格式显示日期和 UTC 时间。这很酷,但是有没有办法让它说“几秒钟前”、“几分钟前”、“几小时前”、“几天前”。而不是确切的日期和时间?提前致谢!更新代码:仍然不起作用,现在字段也不会填充 :(

<!DOCTYPE html>
 <html>
 <head>
 <link href='style.css?lol=<?php echo time(); ?>' rel='stylesheet' 
 type='text/css'>

 </head>
 <body>
     
      <script type='text/javascript'>
    setInterval(function() {
        var date = new Date();
        var fy = date.getUTCFullYear();
        var tm = date.getUTCMonth() + 1;
        var td = date.getUTCDate();
        var h = date.getUTCHours();
        var m = date.getUTCMinutes();
        var s = date.getSeconds();
        tm = checkTime(tm);
        td = checkTime(td);
        m = checkTime(m);
        s = checkTime(s);
        $('#time_id').html(fy + "-"+ tm + "-" + td + " " + h + ":" + m + ":" + s );
    }, 500);
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
</script>


 <?php
 
    $curenttime = $table['posted_time'];
    $time_ago = strtotime($curenttime);
 
 function timeAgo($time_ago) {
    $cur_time = time();
    $time_elapsed = $cur_time - $time_ago;
    $seconds = $time_elapsed ;
    $minutes = round($time_elapsed / 60 );
    $hours = round($time_elapsed / 3600);
    $days = round($time_elapsed / 86400 );
    $weeks = round($time_elapsed / 604800);
    $months = round($time_elapsed / 2600640 );
    $years = round($time_elapsed / 31207680 );
    if ($seconds <= 60) {
        echo "just now";
    } else if ($minutes <= 60) {
        if ($minutes == 1) {
            echo "1 minute ago";
        } else {
            echo "$minutes"." minutes ago";
        }
    } else if ($hours <= 24) {
        if ($hours == 1) {
            echo "1 hour ago";
        } else {
            echo "$hours"." hours ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            echo "yesterday";
        } else {
            echo "$days"." days ago";
        }
    } else if ($weeks <= 4.3) {
        if ($weeks == 1) {
            echo "1 week ago";
        } else {
            echo "$weeks"." weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            echo "1 month ago";
        } else {
            echo "$months"." months ago";
        }
    } else {
        if ($years == 1) {
            echo "1 year ago";
        } else {
            echo "$years"." years ago";
        }
    }
}

 include_once("dbConnect.php");
 if (isset($_GET["frompost"])) {
 echo "<h3>Thank you for your scrim post! It's been tweeted by <a 
 href='http://twitter.com/scrimfinder'>ScrimFinder</a>.";   
 echo "<br>";
 } else {
 }
 echo '<table cellspacing="0" cellpadding="10" width="1"><tr class="top">  
 <th>System</th>
 <th>Region</th>
 <th>Game</th>
 <th>Match Type</th>
 <th>Time Posted (UTC)</th>
 <th>Individual or Team?</th>
 <th>Gamertag</th>
 <th>Twitter</th>
 </tr>';  
 $sql = "SELECT * FROM scrims ORDER BY id DESC LIMIT 15;";  
 $result = mysqli_query($conn, $sql);  
 if(mysqli_num_rows($result)  > 0):
 while ($row = mysqli_fetch_array($result) ) {    
 echo '        
 <tr><td><center>'.$row["system"].'</center></td><td class="grey">
 <center>'.$row["region"].'</center></td>  <td>
 <center>'.$row["game"].'</center></td>  <td class="grey"> 
 <center>'.$row["matchtype"].'</center></td>  <td>
 <center>'.$row["timeAgo($time_ago)"].'</center></td>  <td class="grey">
 <center>'.$row["Ind_Team"].'</center></td>  <td class="grey">
 <center>'.$row["gamertag"].'</center></td>  <td><center><a 
 href="http://www.twitter.com/'.$row['twitter'].'" target="_blank"><input 
 type="submit" value="Message '.$row['twitter'].'" class="button" style="white-space:normal;"></a>
 </center></td>          </tr>  ';  }
 else:
 echo "<tr><td colspan = '100'>NO RECORDS FOUND</td></tr>";
 endif;
 echo '</table>';
 ?>

最佳答案

您可以使用 momentJS并获得您在问题中提到的值。

moment("20111031", "YYYYMMDD").fromNow(); // 6 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow();        // an hour ago
moment().endOf('day').fromNow();          // in a day
moment().startOf('hour').fromNow();       // 10 Minutes ago

根据@MEE的评论和PHP问题,我建议你引用下面的链接,

momantJS php library

php integration

关于php - 如何显示 "minutes ago"而不是 posted_time 的 UTC 时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833777/

上一篇:html - Logo 下方的 Bootstrap 3 导航栏

下一篇:css - 如何缩小 img 以适应其 flex 容器?

相关文章:

php - 在 WooCommerce 电子邮件通知中显示多个分组的自定义字段

JQuery:如何替换某些字符之间的所有字符?

html - css 定位,图像被剪切

css - 响应字体大小 : Which values are appropriate?

php - 使用AJAX显示php生成的图像

php - Cakephp MYSQL查询: SELECT where date is greater than

php - Laravel Lucid体系结构依赖项注入(inject)

jquery - Knockoutjs 和模板 : How to inject a string template without involving the DOM?

javascript - jQuery 延迟触发功能

css - 从 ppi 计算像素大小