change echo to return in the function time_stamp
How to call variable from function and echo it allready in echo
-
18-10-2022 - |
سؤال
I have a code made by me, and a "Time ago" code made by another coder...
Problem pic:
So my code grabs timestamp from database:
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>";
but as you see times are not in tables, and I don't know how to fix it.
I used this code to convert:
<?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";
}
}
}
?>
I just need to echo converted time to table.
المحلول
نصائح أخرى
Your function echoes the result instead of returning it. You have two solutions :
- Rewrite the function so that it returns the result.
- Accomodate your code to the function.
The latter could look like this :
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>";
In your script, please pass the time-stamp value that you have read from database. You have mentioned the variable name there, please replace it with the time-stamp value of corresponding row.
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>"
where index is the column number in which your timestamp is located. Change the time_stamp function to return the timestamp rather than echoing it.
Generally it is good idea to return the value from function instead of echoing it as hugo jan advises. I recommend to get used to this pattern. However, if you don't want/wish to overwrite the time_stamp
code for some reason, you can also use 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>";