“很久以前”来自 MySQL 时间戳
我正在尝试制作一个结果表,其中包括数据库中每个条目的时间戳。但是,我需要将时间显示为“X 秒/分钟/小时前”。
我已经尝试过 jQuery timeago 插件,但成功率为零,现在我想知道如何为每个条目执行此操作。
$result = mysql_query("SELECT * from realmfeed order by ID asc");
echo "<table class='display'>\n";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><b>".$row['ID']."</b></td>\n";
echo "<td>".$row['eventType']."</td>\n";
echo "<td>".$row['server']."</td>\n";
echo "<td>".$row['realm']."</td>\n";
echo "<td>".$row['name']."</td>\n";
echo "<td>".$row['time']."</td>\n</tr>\n\n";
}
echo "</table>\n";
如何为每个结果创建一个“时间之前”函数?
I'm trying to make a table of results including the timestamp for each entry from the database. However, I need the time to be displayed as "X seconds/minutes/hours ago".
I've tried the jQuery timeago plugin with zero success and am now wondering how to do this for each entry.
$result = mysql_query("SELECT * from realmfeed order by ID asc");
echo "<table class='display'>\n";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><b>".$row['ID']."</b></td>\n";
echo "<td>".$row['eventType']."</td>\n";
echo "<td>".$row['server']."</td>\n";
echo "<td>".$row['realm']."</td>\n";
echo "<td>".$row['name']."</td>\n";
echo "<td>".$row['time']."</td>\n</tr>\n\n";
}
echo "</table>\n";
How is it possible to create a "time ago" function for each result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅 DateInterval 了解可用的函数和字段
please refer to DateInterval for available functions and fields
使用 PHP 进行时差计算的问题是您假设 MySQL 和 PHP 服务器位于同一时区。最好在 SELECT 中进行差异计算:
SELECT TIMEDIFF(NOW(), `time`) AS ago FROM Realmfeed ORDER BY ID ASC
这将返回
hh:mm:ss
> 格式化时差。或者,如果您只想要秒数:
SELECT TIME_TO_SEC(TIMEDIFF(NOW(), `time`)) ASSecondsAgo FROM Realmfeed ORDER BY ID ASC
The problem with using PHP to do the time difference calculations is you're assuming the MySQL and PHP servers are in the same time zones. Better to do the difference calculation in the SELECT:
SELECT TIMEDIFF(NOW(), `time`) AS ago FROM realmfeed ORDER BY ID ASC
This will return
hh:mm:ss
formatted time difference.Or if you'd prefer just the number of seconds:
SELECT TIME_TO_SEC(TIMEDIFF(NOW(), `time`)) AS secondsAgo FROM realmfeed ORDER BY ID ASC
我会从 MYSQL 获取时间戳,然后在 php 中进行数学计算,例如。
这里有一个很好的函数,您可以使用差异,尽管我自己没有检查过......
http:// /itwigle.com/twig/PHP_Time_Ago_Function
I would get the time stamp from MYSQL then do the maths in php eg.
There is a nice function here you could use the difference on although I have not checked it myself.....
http://itwigle.com/twig/PHP_Time_Ago_Function
试试这个可能有帮助
try this this may help