计时器倒计时(困惑)


Timer Countdown (Confused)

基本上,我正在尝试在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->diff($date);然后只需回显 $diff->h 例如一小时。但我对这件事感到困惑。

您可以使用

DateTime::d iff函数:

<?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