*/ /** * patTemplate modfifier Date 2 Age * * Date string or unix time stamp to age in seconds * * @package patTemplate * @package Modifiers * @author gERD Schaufelberger */ class patTemplate_Modifier_Date2age extends patTemplate_Modifier { /** * modify the value * * @param string value * @return string modified value */ public function modify($value, $params = array()) { // convert unix timestamp to ISO date if (!preg_match('/^[0-9]+$/', $value)) { $value = strtotime($value); } $compare = time(); if (isset($params['compare'])) { $compare = $params['compare']; if (!preg_match('/^[0-9]+$/', $compare)) { $compare = strtotime($compare); } } $age = $compare - $value; // calculate age in minuts, hours, days, ... if (!isset($params['unit'])) { $params['unit'] = 's'; } // use fall-through switch ($params['unit']) { case 'w': case 'week': $age = $age / 7; case 'd': case 'day': $age = $age / 24; case 'h': case 'hour': $age = $age / 60; case 'i': case 'minute': $age = $age / 60; default: case 's': case 'second': break; } if (!isset($params['precision'])) { $params['precision'] = '0'; } switch ($params['precision']) { case 'ceil': return ceil($age); break; case 'floor': return floor($age); break; } $params['precision'] = intval($params['precision']); return round($age, $params['precision']); } }