Zend_View_Helper_Duration
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 | class Zend_View_Helper_Duration extends Zend_View_Helper_Abstract { public function duration(Zend_Measure_Time $duration) { $this->duration = $duration->setType(\Zend_Measure_Time::SECOND); return clone $this; } public function setFormat(\SplString $format) { $this->format = $format; return $this; } public function setResolution($resolution) { $this->size = array_search($resolution, $this->resolutions); if (false === $this->size) { $this->size = 1; } return $this; } public function setPrecision(\SplInt $precision) { $this->precision = $precision; return $this; } public function render() { $specifiers = array_combine(array_keys($this->resolutions), array ('y','w','d','h','m','s')); $output = $this->format; $chunks = $this->chunk(); foreach ($specifiers as $size => $specifier) { $count = preg_match("/%([^ywdhmsD]*)$specifier/", $output, $matches); if (0 < $count) { $letter = ($size === $this->size && 0 < $this->precision ? 'f' : 'd'); $output = str_replace($matches[0], sprintf('%' . $matches[1] . $letter, $chunks[$size]), $output); } } $count = preg_match("/%([^ywdhmsD]*)D/", $output, $matches); if (0 < $count) { $output = str_replace($matches[0], sprintf('%' . $matches[1] . 'd', (int)$this->duration->getValue()), $output); } return $output; } public function __toString() { return $this->render(); } protected $duration; protected $format = '%D'; protected $size = 1; protected $precision = 0; protected $resolutions = array ( 31449600 => Zend_Measure_Time::YEAR, 604800 => Zend_Measure_Time::WEEK, 86400 => Zend_Measure_Time::DAY, 3600 => Zend_Measure_Time::HOUR, 60 => Zend_Measure_Time::MINUTE, 1 => Zend_Measure_Time::SECOND, ); protected function chunk() { $chunks = array_combine(array_keys($this->resolutions), array (0,0,0,0,0,0)); $remainder = (int)$this->duration->getValue(); foreach ($this->resolutions as $size => $resolution) { if ($seconds <= $remainder) { $chunks[$size] = $value = floor($remainder / $size); $remainder -= ($size * $value); } else { $chunks[$size] = 0; } if ($size === $this->size || 1 === $size) { $chunks[$size] += round($remainder / $size, $this->precision); break; } } return $chunks; }} ?> |
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
Render duration ("relative times") in format driven by sprintf()-like specifiers.
Example usage:
Show 67 minutes as fractional hour formatted to 2 decimal places:
<?php
echo $this->duration(new Zend_Measure_Time(67, Zend_Measure_Time::MINUTE))
->setFormat('%.2h')
->setResolution(Zend_Measure_Time::HOUR)
->setPrecision(2)
?>
Show as "H hours M minutes S seconds":
<?php
echo $this->duration(new Zend_Measure_Time(67, Zend_Measure_Time::MINUTE))
->setFormat('%h hours %m minutes %s seconds')
?>
Snippet details
- Created:
-
bishop
- Edited:
-
bishop
- Revision Id:
- 228
- Edit Message:
- Initial Release
- ZF Version
- 1.7.0
- Tags:
- view helper Zend_Measure_Time Zend_View_Helper duration time interval
- Comments:
- 1
- Views:
- 2618
- Points:
- 0 (0 votes)
1 year ago
Similar to:
http://www.zfsnippets.com/snippets/view/id/43
http://www.zfsnippets.com/snippets/view/id/39
Discussion of object-oriented view helper at:
http://www.ideacode.com/content/super-charged-view-helpers