带有当前时间戳的时间早于php

tez616oj  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(200)

我是法国人,这篇文章来自一个在线翻译。我为拼写错误提前道歉。
上下文:我试图在一个页面中显示我的文章的创建时间和当天日期之间的时间差。制作一个类似于facebook的系统,用于发布评论。
例如“1天前”。
问题:当我在文章中应用我的函数时,它总是在几分钟后显示“刚才”。我可以挖我的头,但我不明白。你能带我去一条小路吗?
以下是可以帮助您的要素。
我的文章类中的方法:

enter code here  public function getDate_creation() {
    return $this->date_creation;
}

public function articleTimeAgo()
{
    $time_ago = strtotime($this->getDate_creation());
    $current_time = time();
    $time_difference = $current_time - $time_ago;
    $seconds = $time_difference;
    $minutes      = round($seconds / 60 );           // value 60 is seconds
    $hours           = round($seconds / 3600);           //value 3600 is 60 minutes * 60 sec
    $days          = round($seconds / 86400);          //86400 = 24 * 60 * 60;
    $weeks          = round($seconds / 604800);          // 7*24*60*60;
    $months          = round($seconds / 2629440);     //((365+365+365+365+366)/5/12)*24*60*60
    $years          = round($seconds / 31553280);     //(365+365+365+365+366)/5 * 24 * 60 * 60
    if($seconds <= 60)
    {
        return "Just Now";
    }
    else if($minutes <=60)
    {
        if($minutes==1)
        {
            return "one minute ago";
        }
        else
        {
            return "$minutes minutes ago";
        }
    }
    else if($hours <=24)
    {
        if($hours==1)
        {
            return "an hour ago";
        }
        else
        {
            return "$hours hrs ago";
        }
    }
    else if($days <= 7)
    {
        if($days==1)
        {
            return "yesterday";
        }
        else
        {
            return "$days days ago";
        }
    }
    else if($weeks <= 4.3) //4.3 == 52/12
    {
        if($weeks==1)
        {
            return "a week ago";
        }
        else
        {
            return "$weeks weeks ago";
        }
    }
    else if($months <=12)
    {
        if($months==1)
        {
            return "a month ago";
        }
        else
        {
            return "$months months ago";
        }
    }
    else
    {
        if($years==1)
        {
            return "one year ago";
        }
        else
        {
            return "$years years ago";
        }
    }
}

我的观点:

<?php if (sizeof($articles) > 0) :
                    foreach ($articles as $article) : ?>
                        <div class="children">
                            <h3><?php echo (empty($article->getAuteur()) ? 'Anonyme' : $article->getAuteur()) ; ?></h3>
                            <p><?php echo $article->getDescription(); ?></p>
                            <span><p><?php echo $article->articleTimeAgo(); ?></p></span>
                        </div>
                    <?php endforeach;
                endif; ?>

我对数据库中字段的格式有疑问,所以我给您提供了“创建日期”字段的格式:
类型=时间戳值=当前时间戳。
我希望我已经说得够清楚了。我随时为您提供任何其他信息。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题