“很久以前”来自 MySQL 时间戳

发布于 2025-01-04 03:07:13 字数 773 浏览 5 评论 0原文

我正在尝试制作一个结果表,其中包括数据库中每个条目的时间戳。但是,我需要将时间显示为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀念你的温柔 2025-01-11 03:07:13
$time = strtotime($row['time']);
$dbDate = new DateTime($time);
$currDate = new DateTime();
$interval = $currDate->diff($dbDate);
echo $interval->d." days ".$interval->h." hours";

请参阅 DateInterval 了解可用的函数和字段

$time = strtotime($row['time']);
$dbDate = new DateTime($time);
$currDate = new DateTime();
$interval = $currDate->diff($dbDate);
echo $interval->d." days ".$interval->h." hours";

please refer to DateInterval for available functions and fields

一个人的旅程 2025-01-11 03:07:13

使用 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

七颜 2025-01-11 03:07:13

我会从 MYSQL 获取时间戳,然后在 php 中进行数学计算,例如。

$result = mysql_query("SELECT ..., UNIX_TIMESTAMP(time) as time FROM realfeed ORDER BY ID ASC");


$row = mysql_fetch_array($result);
$then = $row['time'];
$now = time();

$diff = $now - $then; //Now you have the difference in seconds

这里有一个很好的函数,您可以使用差异,尽管我自己没有检查过......

http:// /itwigle.com/twig/PHP_Time_Ago_Function

I would get the time stamp from MYSQL then do the maths in php eg.

$result = mysql_query("SELECT ..., UNIX_TIMESTAMP(time) as time FROM realfeed ORDER BY ID ASC");


$row = mysql_fetch_array($result);
$then = $row['time'];
$now = time();

$diff = $now - $then; //Now you have the difference in seconds

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

知足的幸福 2025-01-11 03:07:13

试试这个可能有帮助

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="jquery.timeago.js"></script>
</head>

<body>
<?php 
$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><abbr class=\"timeago\" title=\"".date("j F Y h:i:s A",$row['time'])."\"></abbr></td>\n</tr>\n\n";
}


echo "</table>\n";
?>

    <script>
    jQuery(document).ready(function($){
     $("abbr.timeago").timeago()

    });
    </script>

</body>
</html>

try this this may help

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="jquery.timeago.js"></script>
</head>

<body>
<?php 
$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><abbr class=\"timeago\" title=\"".date("j F Y h:i:s A",$row['time'])."\"></abbr></td>\n</tr>\n\n";
}


echo "</table>\n";
?>

    <script>
    jQuery(document).ready(function($){
     $("abbr.timeago").timeago()

    });
    </script>

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文