我有一个由我制作的代码,以及另一个编码器制作的“Time ago”代码......
问题图片:
所以我的代码从数据库中获取时间戳:
echo "<h1> User hash: " . $link . "</h1><br>
<table border='1' width=100% BORDERCOLOR=LIME bgcolor='#000000'>
<tr>
<th>IP</th>
<th>Time</th>
<th>Browser</th>
<th>More info</th>
</tr>";
include 'timeago.php';
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
$time_ago = "{$row[1]}";
echo "
<tr>
<td align='center'>{$row[0]}</td>
<td align='center'>" .time_stamp($time_ago). "</td>
<td align='center'>{$row[3]}</td>
<td align='center'><a href='http://whatismyipaddress.com/ip/{$row[0]}' target='_blank'> More info here </a></td>
</tr>";
}
echo "</table>";
但是你看到时间不在表格中,我不知道如何修复它。
我用这段代码转换:
<?php
//Php Time_Ago Script v1.0.0
//Scripted by D.Harish Kumar@TYSON567
function time_stamp($time_ago)
{
$cur_time=time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60)
{
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60)
{
if($minutes==1)
{
echo "one minute ago";
}
else
{
echo "$minutes minutes ago";
}
}
//Hours
else if($hours <=24)
{
if($hours==1)
{
echo "an hour ago";
}
else
{
echo "$hours hours ago";
}
}
//Days
else if($days <= 7)
{
if($days==1)
{
echo "yesterday";
}
else
{
echo "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3)
{
if($weeks==1)
{
echo "a week ago";
}
else
{
echo "$weeks weeks ago";
}
}
//Months
else if($months <=12)
{
if($months==1)
{
echo "a month ago";
}
else
{
echo "$months months ago";
}
}
//Years
else
{
if($years==1)
{
echo "one year ago";
}
else
{
echo "$years years ago";
}
}
}
?>
我只需要将转换时间回显到表格。
答案 0 :(得分:2)
将echo更改为函数time_stamp
中的返回值答案 1 :(得分:1)
您的函数会回显结果而不是返回结果。你有两个解决方案:
后者可能如下所示:
echo "
<tr>
<td align='center'>{$row[0]}</td>
<td align='center'>";
time_stamp($time_ago);
echo "</td>
<td align='center'>{$row[3]}</td>
<td align='center'><a href='http://whatismyipaddress.com/ip/{$row[0]}' target='_blank'> More info here </a></td>
</tr>";
答案 2 :(得分:1)
在您的脚本中,请传递您从数据库中读取的时间戳值。您已在此处提及变量名称,请将其替换为相应行的时间戳值。
echo "
<tr>
<td align='center'>{$row[0]}</td>
<td align='center'>" .time_stamp($row[index]). "</td>
<td align='center'>{$row[3]}</td>
<td align='center'><a href='http://whatismyipaddress.com/ip/{$row[0]}' target='_blank'> More info here </a></td>
</tr>"
其中index是时间戳所在的列号。更改time_stamp函数以返回时间戳而不是回显它。
答案 3 :(得分:1)
一般来说,最好从函数返回值,而不是像hugo jan建议的那样回显它。我建议习惯这种模式。但是,如果您出于某种原因不希望/希望覆盖time_stamp
代码,您还可以使用output buffering
$time_ago = "{$row[1]}";
ob_start(); // turn on buffering
time_stamp($time_ago); // echo result into buffer
$time = ob_get_contents(); // store buffer into var
ob_end_clean(); // clean buffer
echo "
<tr>
<td align='center'>{$row[0]}</td>
<td align='center'>" .$time. "</td>
<td align='center'>{$row[3]}</td>
<td align='center'><a href='http://whatismyipaddress.com/ip/{$row[0]}' target='_blank'> More info here </a></td>
</tr>";