【问题标题】:How to call variable from function and echo it allready in echo如何从函数中调用变量并在 echo 中回显它
【发布时间】:2014-04-02 00:57:40
【问题描述】:

我有一个由我编写的代码,以及一个由另一个编码器编写的“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";
}
}
}
?>

我只需要将转换后的时间回显到表格中。

【问题讨论】:

    标签: php mysql time html-table converter


    【解决方案1】:

    在函数time_stamp中改变echo以返回

    【讨论】:

    • 感谢这位朋友,我在 google 上搜索 appx 2 小时 :D 你拯救了我的一天 :D 等待 9 分钟以标记为答案 :)
    【解决方案2】:

    您的函数回显结果而不是返回结果。你有两个解决方案:

    1. 重写函数,使其返回结果。
    2. 让您的代码适应函数。

    后者可能如下所示:

      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>"; 
    

    【讨论】:

      【解决方案3】:

      在您的脚本中,请传递您从数据库中读取的时间戳值。您已经提到了变量名称,请将其替换为相应行的时间戳值。

      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 函数以返回时间戳而不是回显它。

      【讨论】:

        【解决方案4】:

        一般来说,从函数返回值而不是像 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>"; 
        

        【讨论】:

        • 我刚刚使用了 replace :) 用 return 替换了每个 echo ,瞧,代码可以工作了 :)
        猜你喜欢
        • 2019-08-19
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 2015-05-02
        • 1970-01-01
        • 2015-08-14
        • 2013-06-07
        • 1970-01-01
        相关资源
        最近更新 更多