想要将 strtotime 值与当前时间进行比较并输出: x 分钟前的内容
我正在开发类似于 Facebook 的新闻源。我将 mysql 表中发生某事的时间与当前时间进行比较并输出:x 分钟前发生某事。
首先,我使用以下代码连接到 mysql:
$conn = db_connect();
$newsfeed = $conn->query("select info, username, time from newsfeed ORDER BY time DESC LIMIT 10");
if (!$newsfeed) {
//die(msg(0,"Could not execute query"));
}
我是 mysql 新手,所以我不确定这是否正确。我希望数据库中的最后 10 个更新与下一个函数一起显示,其中最新更新位于其他函数之上:
$newsfeed_info_array = $newsfeed->fetch_array(MYSQLI_NUM);
$newsfeed_info = $newsfeed_info_array[0];
$newsfeed_username = $newsfeed_info_array[1];
$newsfeed_time= $newsfeed_info_array[2];
$newsfeed_info_split = explode("-", $newsfeed_info); //type and info
date_default_timezone_set('Europe/Paris');
$current_time = strtotime("now");
if ($newsfeed_info_split[0] == "reg")
{
//The next line is the problem
$time_diff = date_diff($current_time, $newsfeed_time);
echo "<p>User $newsfeed_username just registerted ".date( "00:i:s", $newsfeed_time)." min ago</p><br>";
}
if ($newsfeed_info_split[0] == "xxx")
{
echo "blabla x min ago"
}
I am working on a newsfeed like similar to facebook. Where I compare the time when something happened in my mysql table to the current time and output: something something happened x min ago.
First i connect to mysql with this code:
$conn = db_connect();
$newsfeed = $conn->query("select info, username, time from newsfeed ORDER BY time DESC LIMIT 10");
if (!$newsfeed) {
//die(msg(0,"Could not execute query"));
}
I am new to mysql so im not sure of this is correct. I want the last 10 updates in the database to show up with the next functions with the latest updates above the others:
$newsfeed_info_array = $newsfeed->fetch_array(MYSQLI_NUM);
$newsfeed_info = $newsfeed_info_array[0];
$newsfeed_username = $newsfeed_info_array[1];
$newsfeed_time= $newsfeed_info_array[2];
$newsfeed_info_split = explode("-", $newsfeed_info); //type and info
date_default_timezone_set('Europe/Paris');
$current_time = strtotime("now");
if ($newsfeed_info_split[0] == "reg")
{
//The next line is the problem
$time_diff = date_diff($current_time, $newsfeed_time);
echo "<p>User $newsfeed_username just registerted ".date( "00:i:s", $newsfeed_time)." min ago</p><br>";
}
if ($newsfeed_info_split[0] == "xxx")
{
echo "blabla x min ago"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 unix_timespan,因此只需使用基本数学即可:) Unix 时间跨度是自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数。 http://en.wikipedia.org/wiki/Unix_time
或者,您可以创建一个函数打印秒、分钟、小时等
Since you're using unix_timespan, it's only a matter of using basic maths :) Unix timespan is the number of seconds since January 1 1970 00:00:00 UTC. http://en.wikipedia.org/wiki/Unix_time
Alternatively you can make a function that prints seconds, minutes, hours etc
我使用这个(改编自 Codeigniter 论坛 - 更好的日期):
I use this (adapted from Codeigniter Forums - Nicer Dates):