在php中按日期和时间显示最新评论 [英] display most recent comments by date and time in php

查看:130
本文介绍了在php中按日期和时间显示最新评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发表评论时,我在显示最新评论时遇到问题,一小时后我看到输出为"just now",但我想显示1小时,并显示最近一次的所有评论 我怎么显示

I have facing problem in display to most recent comment when i comment and after 1 hour i see the output is "just now" but i want to display 1 hour a go and display all comments by recent time how can i display

在这种情况下请帮助我

这是我的代码:

  $comments = mysqli_query($conn, "SELECT * FROM `store_review` WHERE store_id = '$id1' ORDER BY review_date DESC ");

  $new_result1 = mysqli_num_rows($comments);

 $time = $row['review_date']." ".$row['review_time'];

 <div class="review-block-date"><?php echo $time; ?><br/><?php echo ago($time); ?></div>

这是我的功能:

<?php
function ago($mytime)
        {             
            $time_ago1 = strtotime($mytime);
            $cur_time   = time();
            $time_elapsed   = $cur_time - $time_ago1;
            $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)
            {
                $post_time = "just now";
            }
            //Minutes
            else if($minutes <=60)
            {
                if($minutes==1)
                {
                   $post_time = "1 minute ago";
                }
                else
                {
                   $post_time = "$minutes minutes ago";
                }
            }
            //Hours
            else if($hours <=24)
            {
                if($hours==1)
                {
                    $post_time = "1 hrs ago";
                }
                else
                {
                    $post_time = "$hours hrs ago";
                }
            }
            //Days
            else if($days <= 7)
            {
                if($days==1)
                {
                    $post_time = "yesterday";
                }
                else
                {
                    $post_time = "$days days ago";
                }
            }
            //Weeks
            else if($weeks <= 4.3)
            {
                if($weeks==1)
                {
                    $post_time = "1 week ago";
                }
                else
                {
                    $post_time = "$weeks weeks ago";
                }
            }
            //Months
            else if($months <=12)
            {
                if($months==1)
                {
                   $post_time = "1 month ago";
                }else{
                    $post_time = "$months months ago";
                }
            }
            //Years
            else
            {
                if($years==1)
                {
                    $post_time = "1 year ago";
                }
                else
                {
                    $post_time = "$years years ago";
                }
            }
            return $post_time;
        }?>

推荐答案

尝试此方法

<?php
function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

echo time_elapsed_string('2017-06-01 00:22:35');

echo "for full";

echo time_elapsed_string('2017-06-01 00:22:35',true);

PHP小提琴

这篇关于在php中按日期和时间显示最新评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆