Zend Framework Source Code Snippets

Bgy_View_Helper_RelativeDateTime

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
<?php
class Bgy_View_Helper_RelativeDateTime extends Zend_View_Helper_Abstract
{
    const YEAR = 'YEAR';
    const MONTH = 'MONTH';    const WEEK = 'WEEK';
    const DAY = 'DAY';
    const HOUR = 'HOUR';
    const MINUTE = 'MINUTE';
    const SECOND = 'SECOND';    const YEARS = 'YEARS';
    const MONTHS = 'MONTHS';
    const WEEKS = 'WEEKS';
    const DAYS = 'DAYS';
    const HOURS = 'HOURS';    const MINUTES = 'MINUTES';
    const SECONDS = 'SECONDS';
 
    protected $_unitTemplates = array(
        self::YEAR => '%value% year',        self::MONTH => '%value% month',
        self::WEEK => '%value% week',
        self::DAY => '%value% day',
        self::HOUR => '%value% hour',
        self::MINUTE => '%value% minute',        self::SECOND => '%value% second',
        self::YEARS => '%value% years',
        self::MONTHS => '%value% months',
        self::WEEKS => '%value% weeks',
        self::DAYS => '%value% days',        self::HOURS => '%value% hours',
        self::MINUTES => '%value% minutes',
        self::SECONDS => '%value% seconds',
    );
     public function __construct($config = null)
    {
 
    }
     public function relativeDateTime(Zend_Date $date = null)
    {
        if (null === $date) {
            return $this;
        } 
        $todayDate = new Zend_Date();
        $diff = $todayDate->sub($date);
 
        $mt = new Zend_Measure_Time($diff);        $units = $mt->getConversionList();
 
        $chunks = array(
            Zend_Measure_Time::YEAR,
            Zend_Measure_Time::MONTH,            Zend_Measure_Time::WEEK,
            Zend_Measure_Time::DAY,
            Zend_Measure_Time::HOUR,
            Zend_Measure_Time::MINUTE,
            Zend_Measure_Time::SECOND,        );
 
        for ($i = 0, $count = count($chunks); $i < $count; ++$i) {
            $seconds = $units[$chunks[$i]][0];
            $unitKey = $chunks[$i];            if (0.0 !== ($result = floor($diff->get(Zend_Date::TIMESTAMP) / $seconds))) {
                break;
            }
        }
         $translateHelper = new Zend_View_Helper_Translate();
        if ($result === (float)1) {
            $formatedString = $translateHelper->translate($this->getUnitTemplate($unitKey));
        } else {
            $formatedString = $translateHelper->translate($this->getUnitTemplate($unitKey.'S'));        }
        $formatedString = str_replace('%value%', (string) $result, $formatedString);
 
        return $formatedString;
    } 
    public function setUnitTemplate($template, $unitKey)
    {
        if (!isset($this->_unitTemplates[$unitKey])) {
            require_once 'Bgy/Helper/Exception.php';            throw new Zend_Validate_Exception("No unit template exists for key '$unitKey'");
        }
 
        $this->_unitTemplates[$unitKey] = $template;
         return $this;
    }
 
    public function setUnitTemplates(array $templates)
    {        foreach ($templates as $key => $template) {
            $this->setUnitTemplate($template, $key);
        }
        return $this;
    } 
    public function getUnitTemplates()
    {
        return $this->_unitTemplates;
    } 
    public function getUnitTemplate($unitKey)
    {
        if (!isset($this->_unitTemplates[$unitKey])) {
            require_once 'Bgy/Helper/Exception.php';            throw new Bgy_Helper_Exception("No unit template exists for key '$unitKey'");
        }
 
        return $this->_unitTemplates[$unitKey];
    }}

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Format a Zend_Date object to a relative date/time in the biggest unit possible.

Use Zend_View_Helper_Translate to translate message.

Message are configurable with setUnitTemplate() method (see source) and works similar to the Validator custom templates.

Returns something like this:

1 year,
9 months,
2 weeks,
6 days,
15 hours,
50 minutes,
20 secondes

Snippet details

Created:
Boris Guéry Boris Guéry
1 year ago
Edited:
Boris Guéry Boris Guéry
1 year ago
Revision Id:
179
Edit Message:
Initial Release
ZF Version
1.10.7
Tags:
date view helper time datetime relative
Comments:
0
Views:
3647
Points:
1 (1 votes)

History

r179

Initial Release

Boris Guéry Boris Guéry
1 year ago
diff
r178

Initial Release

Boris Guéry Boris Guéry
1 year ago
diff
r177

Initial Release

Boris Guéry Boris Guéry
1 year ago