我正在尝试在聊天中显示“{number} {unit-of-time} ago”。但是,当我期待“2分钟前”这样的事情时,我会看到“45年”。
没有人有答案?
这是脚本:
<?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>";
}
?>
我该怎么做让这个功能起作用?
答案 0 :(得分:2)
首先使用预定义函数 date()在mysql数据库中存储消息发布的时间和日期,以获取本地时间,我们必须在date之前使用 date_default_timezone_set()函数)
date_default_timezone_set("Asia/Calcutta");
$massege_post_time=date("H:i:s Y-m-d ");
时间和日期将存储在数据库中,例如2018-08-09 15:56:28
其中H = 15(24小时制)i = 56(分钟)s(秒)= 28
Y(年)= 2018 m(月)= 08,d(天)= 09
现在我们得到当前时间与存储在数据库中的消息的发布时间之间的差,因此我们得到了并将结果存储在$ post_tme_msg变量中
这是完整的代码
//get the time from database replace “15:56:28 2018-08-09” with post time message
$post_tme_msg="13:00:28 2018-08-09";
//get the current time
date_default_timezone_set("Asia/Calcutta");
$time_now=date("H:i:s Y-m-d");
$time_diff_second=(strtotime($time_now)-strtotime($post_tme_msg));
/* time difference we get strtotime() function is in second so we convert it in
minute,hour,minute,hour,day,week,month,year */
$time_diff_minute=($time_diff_second/60);
$time_diff_hour=($time_diff_second/3600);
$time_diff_days=($time_diff_second/(3600*24));
$time_diff_week=($time_diff_second/(3600*24*7));
$time_diff_month=($time_diff_second/(3600*24*30));
$time_diff_year=($time_diff_second/(3600*24*30*12));
// when time difference is less than 1 minute(60 second)
if($time_diff_second<60)
{
$final_created_duration_time="Just now";
}
// when time difference is less than 1hour(60 minute)
elseif($time_diff_minute<60 && $time_diff_minute>0)
{
$created_duration_time=round($time_diff_minute,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' minute ago';
}
//when time difference is less than 1 day(24hour)
elseif($time_diff_hour<24 && $time_diff_hour>0)
{
$created_duration_time=round($time_diff_hour,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' hours ago';
}
// when time difference is less than 1 week(7 days)
elseif($time_diff_days<7 && $time_diff_days>0)
{
$created_duration_time=round($time_diff_days,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' days ago';
}
//when time difference is less than 1 month(4 week)
elseif($time_diff_week<=4 && $time_diff_week>0)
{
$created_duration_time=round($time_diff_week,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' week ago';
}
// when time difference is less than 1 year(12 month)
elseif($time_diff_month<12 && $time_diff_month>0)
{
$created_duration_time=round($time_diff_month,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' month ago';
}
//when time difference is 1 year or greater than 1 year
else
{
$created_duration_time=round($time_diff_year,0,PHP_ROUND_HALF_DOWN);
$final_created_duration_time=$created_duration_time.' years ago';
}
//$final_created_duration_time is give exact what we want so print it
echo $final_created_duration_time;
您可以对此有更多了解。
尝试以下操作:http://techifind.com/?blog=time-ago-in-php-like-facebook
答案 1 :(得分:0)
您必须获取日期:
$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
将以毫秒为单位。
你在做什么循环和数组?太令人困惑了。
mysql_
!!!