如何计算PHP或Zend_Date的时间? [英] How to calculate time passed with PHP or Zend_Date?

查看:179
本文介绍了如何计算PHP或Zend_Date的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个视图助手,它将显示自...以来已经过去的分钟数,小时数或天数。我不太确定如何做到这一点。看起来日期比较正在工作,但我不知道如何获得数字。这是我迄今为止所做的:

I'm trying to create a view helper that will display the number of minutes, hours, or days that have passed since...right now. I'm not really sure how to do it. It looks like the date comparison is working, but I don't know how to get the number. Here's what I have so far:

<?php

class Zend_View_Helper_RecentDate
{
    public function recentDate($datetime)
    {
        $date = new Zend_Date($datetime);

        switch ($date) {
            case($date->isEarlier(1, Zend_Date::HOUR)):
                $message = 'was minutes ago';
                break;
            case($date->isEarlier(24, Zend_Date::HOUR)):
                $message = 'was hours ago';
                break;
            case($date->isEarlier(48, Zend_Date::HOUR)):
                $message = 'Yesterday';
                break;
            default:
                $message = 'was days ago';
                break;
        }
        return $message;
    }
}

我想将was替换为实际数字分钟/小时/天。

I want to replace "was" with the actual number of minutes/hours/days passed.

推荐答案

由于您已经在使用Zend Framework,因此有一个 TimeSince view helper Zym Framework 中,这是一个额外的ZF扩展和帮助的库。

Since you're already using the Zend Framework, there's a TimeSince view helper in the Zym Framework, which is a library of additional ZF extensions and helpers.

TimeSince 帮助者基本上完全按照你想要做的:

The TimeSince helper does basically exactly what you're trying to do:

Last updated <?= $this->timeSince($timestamp); ?> ago

输出如下:

Last updated 8 hours ago

这篇关于如何计算PHP或Zend_Date的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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