本文介绍了xx时间之前的功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试在聊天中显示"{number} {time-time}前".但是,当我期望"2分钟前"之类的东西时,我会看到"45年".

I am trying to display "{number} {unit-of-time} ago" in my chat. However, when I'm expecting something like "2 minutes ago", I see "45 years" instead.

没人回答吗?

以下是脚本:

<?php
$con = mysql_connect("localhost", "user", "pword");
mysql_select_db('chat', $con);
$result1 = mysql_query("SELECT * FROM logs ORDER BY id ASC");

$tm = mysql_query("SELECT timestamp FROM logs ORDER BY id ASC");

function _ago($tm, $rcs = 0)
{
    $cur_tm = time();
    $dif = $cur_tm - $tm;
    $pds = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year');
    $lngh = array(1, 60, 3600, 86400, 604800, 2630880, 31570560);
    for ($v = sizeof($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--) ;
    if ($v < 0) $v = 0;
    $_tm = $cur_tm - ($dif % $lngh[$v]);

    $no = floor($no);
    if ($no <> 1) $pds[$v] .= 's';
    $x = sprintf("%d %s ", $no, $pds[$v]);
    if (($rcs == 1) && ($v >= 1) && (($cur_tm - $_tm) > 0)) $x .= time_ago($_tm);
    return $x;
}

$timeagochat = _ago($tm, $rcs = 0);

while ($extract = mysql_fetch_array($result1)) {
    echo "<p class='show_message'><span class='uname'>" . $extract['username'] . "</span> <span class='time'>" . $timeagochat . " </span><br><span class='msg'>" . $extract['msg'] . "</span></p><br>";
}

?>

我该怎么做才能使此功能正常工作?

What can I do to let this function work?

推荐答案

  1. 您必须获取日期:

  1. You have to fetch the date:

$tm = mysql_query("SELECT timestamp AS t FROM logs ORDER by id ASC")->fetch_assoc()['t']; //obviously, check the size before you fetch

您可能想使用strtotime(),例如:

$dif = $cur_tm-strtotime($tm);

$dif将以毫秒为单位.

您对循环和数组做什么?太混乱了.

What are you doing with the loops and the arrays? Too confusing.

这篇关于xx时间之前的功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:09