Location: PHPKode > projects > Mocovie web framework > webs/common/controls/duration.php
<?php
/**
 *  Copyright (C) 2010  Kai Dorschner
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Kai Dorschner <the-hide@address.com>
 * @copyright Copyright 2010, Kai Dorschner
 * @license http://www.gnu.org/licenses/gpl.html GPLv3
 * @package mocovi
 * @subpackage controls
 */

/**
 * Require Date control.
 */
ControlFactory::load('date');

/**
 * This is a prototype control.
 *
 * From this class all other controls can be derived.
 *
 * @package mocovi
 * @subpackage controls
 */
class duration_control extends date_control
{
	protected $defaultOptions = array
	( 'textToken'	=> 'DurationYears'
	, 'format'		=> 'Y' // PHP date() format
	, 'from'		=> '' // Interval from eg.: 10.04.1986
	, 'to'			=> 'now' // Interval to eg.: "now" or "*" -> ~24 Years
	);

	/**
	 * @override
	 */
	protected function getText()
	{
		if(strlen($this->getOption('from')) == 0 || strlen($this->getOption('to')) == 0)
			throw new MvcException('Attributes "from" and "to" must have values!');
		$count = $this->formatDate(strtotime($this->getOption('to')) - strtotime($this->getOption('from')));
		$token = Translator::translateTokenByCount
				( $this->getOption('textToken')
				, $count
				)->cloneNode(true);
		$token->insertBefore(new DomText($count.' '), $token->firstChild);
		return $token;
	}

	public function formatDate($unixtime)
	{
		$divisors = array();
		$divisors['s'] = 1;
		$divisors['i'] = 60;
		$divisors['h'] = $divisors['i'] * 60;
		$divisors['G'] = $divisors['h'];
		$divisors['d'] = $divisors['h'] * 24;
		$divisors['W'] = $divisors['d'] * 7;
		$divisors['m'] = $divisors['d'] * 30.4375;
		$divisors['Y'] = $divisors['d'] * 365.25;
		$divisors['y'] = $divisors['Y'];

		$format = $this->getOption('format');
		$return = '';
		for($i = 0; $i < strlen($format); $i++)
		{
			$char = substr($format, $i, 1);
			if(array_key_exists($char, $divisors) && ($i <= 0  || ($i > 0 && substr($format, $i - 1, 1) != '\\')))
			{
				$value = floor($unixtime / $divisors[$char]);
				switch($char)
				{
					case 'G':
						$return .= sprintf('%02d', $value);
					break;
					case 'Y':
						$return .= substr($value, strlen($value) - 2, 2);
					break;
					default:
						$return .= $value;
				}
				$unixtime = $unixtime % $divisors[$char];
			}
			else
				$return .= $char;
		}
		return $return;
	}
}
Return current item: Mocovie web framework