我有一个味精系统,格式为(Sent:08:23:38 pm),我希望它像1分钟前一样,等等。。。这是我从这里得到的信息,但它不能帮助我无法获取来自数据库的时间戳而是尝试处理来自column(timestamp)的值之一,即1467993620,所以基本上我希望它在while循环中工作,而当我将其保留在while循环中时,我会报错
请帮我
$time = strtotime(date('h:i:s a','1467993620'));
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
我刚刚发布了带有while循环和echo时间戳的查询,希望它对您有所帮助
<?php
$req2 = mysql_query('select pm.timestamp, pm.message, user.userID as userid, user.userName, user.userAddress from pm, user where pm.userID="'.$userID.'" and user.userID=pm.user1 order by pm.id2');
while($dn2 = mysql_fetch_array($req2))
{
?>
Sent: <?php date_default_timezone_set('Asia/Kolkata'); echo date('h:i:s a',$dn2['timestamp']); ?> </small> </div>
<br />
<p> <?php echo $dn2['message']; ?> </p>
<?php
}
?>
试试这种格式:
<?php
$time = strtotime(date('h:i:s a','1467993620'));
$time = $time - (1 * 60); // MINUS 60 SECONDS.
$date = date("h:i:s A", $time); // NOTICE THE "A".
echo "Sent: $date";
?>
编辑:humanTiming
用您的新代码调用:
<?php
date_default_timezone_set('Asia/Kolkata');
echo "Sent: " . humanTiming( $dn2['timestamp'] ) . " ago.";
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句