使用mysql fetch数组创建timeago

时间:2017-06-19 22:08:09

标签: php mysql timestamp timeago

我有一个脚本,用于将日期时间值转换为时间标记。我有一个PHP脚本,可以获取管理员的消息行和发送时间。但是,我希望将此时间转换为20分钟之前,因此我想将每条消息的时间转换为某个时间段。所以每次我将timeago脚本添加到mysql查询中时,页面都会拒绝出现。我想这两个剧本并不是齐头并进。

这是我的timeago脚本:

.$rwsm[3].

这是我的mysql查询:

<?php
                   $sql2="SELECT * FROM ".$cn."msg ";
                $result2= mysql_query($sql2) or die(mysql_error());
                        while($rwsm=  mysql_fetch_array($result2)){
                  echo"<li>";
                    echo "<i class='fa  fa-envelope bg-orange'></i>";

                    echo "<div class='timeline-item'>";

//THE TIMEAGO SCRIPT STARTS HERE                
$ptime = $rwsm[3];
function get_timeago( $ptime )
{
    $estimate_time = time() - $ptime;

    if( $estimate_time < 1 )
    {
        return 'less than 1 second ago';
    }

    $condition = array( 
                12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $estimate_time / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'Last Updated: &nbsp; ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';

            echo "$r";
        }
    }
}
$timeago=get_timeago(strtotime($ptime));
echo "<span class=\"time\"><i class=\"fa fa-clock-o\"></i>".$timeago."ago</span>";
//THE TIMEAGO SCRIPT ENDS HERE

    echo "<h3 class='timeline-header'><a href='mailto:".$rwsm[2]."'> ".$rwsm[2]." </a></h3>";

                      $msg= $rwsm[1];
                      echo "<div class='timeline-body'>".$msg."</div>";

                    echo "</div>";
                  echo "</li>";
                      }
                  ?>

这是我尝试将两个脚本组合在一起以将arr1 = [ {name: "Harry", lastname: "Potter"}, {name: "Charlie", lastname: "Brown"}, {name: "Frodo", lastname: "Baggins"} ] 时间变量转换为前一段时间的地方:

arr2 = [
    {bestfriend: "Ron"},
    {bestfriend: "Snoopy"},
    {bestfriend: "Sam"}
]

0 个答案:

没有答案