如何将此时间戳格式(发送时间:08:23:38 pm)显示到“自 1 分钟前”


How Can I display this timestamp format (Sent: 08:23:38 pm) to Since 1min ago

我有一个消息系统,其中格式为(发送时间: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
}
?>
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';
    }
}

}

试试这个格式:

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