我有一个msg系统,其格式为(发送时间:08:23:38 pm),我希望它像1分钟前等... 这是我从这里得到的,但它没有帮助我,我不能从数据库中获取时间戳,而是尝试应对列中的一个值(时间戳),即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
}
?>
答案 0 :(得分:1)
function time_elapsed_string($ptime)
{
$etime = time() - $ptime;
if ($etime < 1)
{
return '0 seconds';
}
$a = array( 365 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
$a_plural = array( 'year' => 'years',
'month' => 'months',
'day' => 'days',
'hour' => 'hours',
'minute' => 'minutes',
'second' => 'seconds'
);
foreach ($a as $secs => $str)
{
$d = $etime / $secs;
if ($d >= 1)
{
$r = round($d);
return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
}
}
}
答案 1 :(得分:0)
请尝试以下格式:
<?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.";
?>