定时器倒计时(困惑)

时间:2015-01-14 16:32:48

标签: php date

基本上,我正在尝试在PHP中进行计时器或倒计时。这将占用线程中保存的时间(发布日期),然后显示与当前时间相比多久以前。

有人知道怎么做吗?

代码:

<?php foreach($this->updated as $thread){ ?>
    <section>
        <div class="body">
            <?php if($this->threads[$thread]['image']!='') { ?>
                <div>
                    <a href="<?php echo $this->threads[$thread]['image']; ?>">
                        <img src="<?php echo $this->threads[$thread]['image']; ?>">
                </a>
                </div>
            <?php } ?>
            <?php echo $this->threads[$thread]['post']; ?>
        </div>
        <div class="small text-right">
            <?php echo count($this->threads[$thread]['posts']); ?> Replies
            &nbsp; | &nbsp;
            <?php echo date("Y-m-d H:i:s", $this->threads[$thread]['time']); ?> EST
            &nbsp; | &nbsp;
            <a href="/<?php echo $thread; ?>">View</a>
        </div>
    </section>
<?php  } ?>

我不希望它是一个精确的计时器,更像它会说“不到一分钟前”,“1分钟前”,“2分钟前”等......

我试过制作2个不同的变量然后做$ current-&gt; diff($ date);然后回声$ diff-&gt; h例如小时。但我对此事感到困惑。

1 个答案:

答案 0 :(得分:0)

您可以使用DateTime::diff功能:

<?php
date_default_timezone_set('Europe/Moscow');
$timestamp = 1421253112;
$date = new DateTime;
$date->setTimestamp($timestamp);

$now = new DateTime;
$interval = $now->diff($date);
echo $interval->format('%i minutes ago');

输出:

7 minutes ago