想要将 strtotime 值与当前时间进行比较并输出: x 分钟前的内容

发布于 2024-09-07 14:06:44 字数 1306 浏览 3 评论 0原文

我正在开发类似于 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 技术交流群。

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

发布评论

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

评论(2

孤蝉 2024-09-14 14:06:44

由于您使用的是 unix_timespan,因此只需使用基本数学即可:) Unix 时间跨度是自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数。 http://en.wikipedia.org/wiki/Unix_time


$postTime = $newsfeed_into_array[2]; 
$now = time();
$seconds = $now - $postTime; // $seconds now contains seconds since post time

$minutes = ceil($span / 60); // minutes since post 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


$postTime = $newsfeed_into_array[2]; 
$now = time();
$seconds = $now - $postTime; // $seconds now contains seconds since post time

$minutes = ceil($span / 60); // minutes since post time

Alternatively you can make a function that prints seconds, minutes, hours etc

酒解孤独 2024-09-14 14:06:44

我使用这个(改编自 Codeigniter 论坛 - 更好的日期):

if( ! function_exists('relative_time'))
{
    function relative_time($datetime)
    {
        if(!$datetime)
        {
            return "no data";
        }

        if(!is_numeric($datetime))
        {
            $val = explode(" ",$datetime);
            $date = explode("-",$val[0]);
            $time = explode(":",$val[1]);
            $datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
        }

        $difference = time() - $datetime;
        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        if ($difference > 0)
        {
            $ending = 'ago';
        }
        else
        {
            $difference = -$difference;
            $ending = 'to go';
        }
        for($j = 0; $difference >= $lengths[$j]; $j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);

        if($difference != 1)
        {
            $period = strtolower($periods[$j].'s');
        } else {
            $period = strtolower($periods[$j]);
        }

        return "$difference $period $ending";
    }


}

I use this (adapted from Codeigniter Forums - Nicer Dates):

if( ! function_exists('relative_time'))
{
    function relative_time($datetime)
    {
        if(!$datetime)
        {
            return "no data";
        }

        if(!is_numeric($datetime))
        {
            $val = explode(" ",$datetime);
            $date = explode("-",$val[0]);
            $time = explode(":",$val[1]);
            $datetime = mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
        }

        $difference = time() - $datetime;
        $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
        $lengths = array("60","60","24","7","4.35","12","10");

        if ($difference > 0)
        {
            $ending = 'ago';
        }
        else
        {
            $difference = -$difference;
            $ending = 'to go';
        }
        for($j = 0; $difference >= $lengths[$j]; $j++)
        {
            $difference /= $lengths[$j];
        }
        $difference = round($difference);

        if($difference != 1)
        {
            $period = strtolower($periods[$j].'s');
        } else {
            $period = strtolower($periods[$j]);
        }

        return "$difference $period $ending";
    }


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