Time ago PHP with Current timestamp

时间:2018-06-10 19:14:43

标签: php html css mysql pdo

我是法国人,本文来自在线翻译。我提前为拼写错误道歉。

上下文:我正在尝试在页面中显示创建文章与当天日期之间的时差。制作类似于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; ?>

我对数据库中字段的格式有疑问,所以我给你字段“date_creation”的格式:

type = timestamp value = CURRENT_TIMESTAMP。

我希望我已经足够清楚了。我会随时为您提供任何其他信息。

0 个答案:

没有答案