如何让“分钟前”显示张贴时间而不是 UTC 时间? [关闭]
Posted
技术标签:
【中文标题】如何让“分钟前”显示张贴时间而不是 UTC 时间? [关闭]【英文标题】:How can I get "minutes ago" to display rather than UTC time for posted_time? [closed] 【发布时间】:2018-07-27 18:37:51 【问题描述】:我用于调用表格的代码以 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" ><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库吗? momentJS 能很好地与 MySQL 配合使用吗?我从来没有使用过那个库...... 对于在 sn-p 中完全可执行且可用于重现问题或有问题的行为。不要对每个代码块都使用 sn-ps,尤其是对于 HTML、CSS 和 JS 以外的其他语言的代码。您可以通过选择某个文本块并按Ctrl+K
或在编辑器中单击此按钮将其标记为代码:&lt;/&gt;
。
@BradyPennington 是的,它会支持,它是一个简单的 JavaScript 库。
【参考方案1】:
您可以使用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
【讨论】:
OP 使用的是 PHP 而不是 JavaScript。请解释一下 OP 如何将这个 JavaScript 库应用到他的 PHP 输出中(当然答案没有错) 这就是我所关心的,根据我的经验,php 并不倾向于与 JS 库配合得很好。但是,如果它解决了我的问题,我愿意使用它:P 你的担心是对的。对不起,我有点糊涂了。顺便说一下,请试一试,顺便参考这个链接,你可以在其中获得 moment 库的 php 版本 - github.com/fightbulc/moment.php。可能会支持 也许这个链接也会对你有所帮助 - ***.com/a/2916189/2820409【参考方案2】:重要,这是我用来获取时间的JS代码
<textarea id="time_id"></textarea>
<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 脚本从 DB 获取时间
我不记得我是从哪里得到这个代码的,所以我不能把作者归功于作者,但我确实在网上找到了这个。
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";
然后你会像这样回显时间:
<?php
$curenttime = $table['date_time'];
$time_ago = strtotime($curenttime);
echo timeAgo($time_ago);
?>
【讨论】:
我试过了,还是不行...检查更新的代码。 您的代码萌芽中有一些重大缺陷@BradyPennington 您的 if 语句的结尾括号以及其他一些我什至无法理解的东西..... 您要提交的表单在哪里? @BradyPennington 我的声望不够。加入不和谐服务器? @BradyPennington discord.gg/SZ9KzT以上是关于如何让“分钟前”显示张贴时间而不是 UTC 时间? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章