如何在php中显示时差

如何在php中显示时差,php,Php,我想以时差的形式显示这些数据,比如2分钟前,2天前 我的代码没有显示正确的时间,而且它在所有发布的数据中显示相同的时间 <?php $conn = new mysqli("localhost","root","","blog_samples"); $sql="UPDATE comments SET status=1 WHERE status=0"; $result=mysqli_query($conn, $sql); try{ $

我想以时差的形式显示这些数据,比如2分钟前,2天前

我的代码没有显示正确的时间,而且它在所有发布的数据中显示相同的时间

 <?php

     $conn = new mysqli("localhost","root","","blog_samples");
     $sql="UPDATE comments SET status=1 WHERE status=0"; 
     $result=mysqli_query($conn, $sql);
     try{
       $con = new PDO("mysql:host=localhost;dbname=blog_samples",'root','');
       $sth = $con->prepare( "SELECT * FROM `comments` ORDER BY id DESC limit 5");
       $sth->setFetchMode(PDO:: FETCH_OBJ);
       $sth->execute();
       while($row=$sth->fetch()){   
         ?>
          <li>
             <a href="javascript:void(0)">
             <span class="label label-primary"><i class="fa fa-user"></i></span>
             <span class="message"> New POST</span>
             <span class="time"><?php
           $tim = $row->samay;
           $date1 = strtotime($tim);
           $date2 = strtotime(date('Y-m-d H:i:s'));
           $seconds_diff = $date2 - $date1;

           echo round(abs($seconds_diff) / 60,2). " mins ago";
           ?></span>
              </a>
           </li>

          <?php } }

    catch(PDOEception $e){
          echo "Error: ". $e->getMessage();
             }

           ?>

  • 使用此功能

    function timeAgo($time_ago) {
            $time_ago = strtotime($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) {
                return "$seconds seconds ago";
            } //Minutes
            else if ($minutes <= 60) {
                if ($minutes == 1) {
                    return "one minute ago";
                } else {
                    return "$minutes minutes ago";
                }
            } //Hours
            else if ($hours <= 24) {
                if ($hours == 1) {
                    return "an hour ago";
                } else {
                    return "$hours hours ago";
                }
            } //Days
            else if ($days <= 7) {
                if ($days == 1) {
                    return "yesterday";
                } else {
                    return "$days days ago";
                }
            } //Weeks
            else if ($weeks <= 4.3) {
                if ($weeks == 1) {
                    return "a week ago";
                } else {
                    return "$weeks weeks ago";
                }
            } //Months
            else if ($months <= 12) {
                if ($months == 1) {
                    return "a month ago";
                } else {
                    return "$months months ago";
                }
            } //Years
            else {
                if ($years == 1) {
                    return "one year ago";
                } else {
                    return "$years years ago";
                }
            }
        }
    
    函数timeAgo($time\u ago){
    $time\u ago=strottime($time\u ago);
    $cur_time=time();
    $time\u appeased=$cur\u time-$time\u ago;
    $seconds=$time\u经过的时间;
    $minutes=round($time_passed/60);
    $hours=整数($time\u appead/3600);
    $days=round($time_passed/86400);
    $weeks=整数($time_passed/604800);
    $months=round($time_passed/2600640);
    $years=round($time_passed/31207680);
    //秒
    
    如果($seconds,则此函数将在一段时间前返回:

    <?php
    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 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
            }
        }
    }
    ?>
    

    这是显示数据代码

     <?php
                                             include('functions.php');
                                             $conn = new mysqli("localhost","root","","blog_samples");
                                             $sql="UPDATE comments SET status=1 WHERE status=0"; 
    $result=mysqli_query($conn, $sql);
                                    try
                                        {
    
                                        $con = new PDO("mysql:host=localhost;dbname=blog_samples",'root','');
    
                                        $sth = $con->prepare( "SELECT * FROM `comments` ORDER BY id DESC limit 5");
    
    
    
                                        $sth->setFetchMode(PDO:: FETCH_OBJ);
    
                                        $sth->execute();
    
                                        while($row=$sth->fetch())
                                        {   
                                        ?>
                                            <li>
                                                <a href="javascript:void(0)">
                                                    <span class="label label-primary"><i class="fa fa-user"></i></span>
                                                    <span class="message"> New POST</span>
                                                    <span class="time"><?php
                                                echo timeAgo(date("Y-m-d H:i:s"));
    
    ?>
                                                   </span>
                                                </a>
                                            </li>
    
    
                                           <?php } }
    
                                catch(PDOEception $e){
                                echo "Error: ". $e->getMessage();
                                    }
    
                                ?>
    
    
    
  • 这是functions.php

    <?php
    
    function timeAgo($time_ago) {
            $time_ago = strtotime($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) {
                return "$seconds seconds ago";
            } //Minutes
            else if ($minutes <= 60) {
                if ($minutes == 1) {
                    return "one minute ago";
                } else {
                    return "$minutes minutes ago";
                }
            } //Hours
            else if ($hours <= 24) {
                if ($hours == 1) {
                    return "an hour ago";
                } else {
                    return "$hours hours ago";
                }
            } //Days
            else if ($days <= 7) {
                if ($days == 1) {
                    return "yesterday";
                } else {
                    return "$days days ago";
                }
            } //Weeks
            else if ($weeks <= 4.3) {
                if ($weeks == 1) {
                    return "a week ago";
                } else {
                    return "$weeks weeks ago";
                }
            } //Months
            else if ($months <= 12) {
                if ($months == 1) {
                    return "a month ago";
                } else {
                    return "$months months ago";
                }
            } //Years
            else {
                if ($years == 1) {
                    return "one year ago";
                } else {
                    return "$years years ago";
                }
            }
        }
    ?>
    
    
    

    所有时间之前都显示相同@Akshay komarla

    请设置可读性格式改进格式如何调用此函数@Akshay komarla
    timeAgo(日期(“Y-m-d H:i:s”))
    只需传递所需的日期和时间编辑所有post的GIV相同输出@Akshay komarlait在所有发布的数据@Milan Chheday中显示相同的时间前你只需发送时间..示例:1516789440..或
    echo get_timeago(time());
    你需要传递所需的日期时间。如果你传递
    echo timeago(日期(“Y-m-d H:i:s”))
    它将给出相同的值,因为
    日期(“Y-m-d H:i:s”)
    将给出当前日期时间。例如:echo timeAgo(日期(“2018-01-23 12:12:12”);像这样使用我获取发布的时间。但它给出相同的输出首先查看ur$times变量打印的格式,然后传递给函数