* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org * @see http://tools.ietf.org/html/rfc3339#section-5.6 */ class Duration extends DateInterval implements \JsonSerializable { public function __construct($duration, $month = null, $day = null, $hour = null, $minute = null, $second = null) { if (func_num_args() == 1) { parent::__construct($this->validate($duration)); } else { $interval = 'P'; $interval.= ((int) $duration) . 'Y'; $interval.= ((int) $month) . 'M'; $interval.= ((int) $day) . 'D'; $interval.= 'T'; $interval.= ((int) $hour) . 'H'; $interval.= ((int) $minute) . 'M'; $interval.= ((int) $second) . 'S'; parent::__construct($interval); } } public function getYear() { return $this->y; } public function getMonth() { return $this->m; } public function getDay() { return $this->d; } public function getHour() { return $this->h; } public function getMinute() { return $this->i; } public function getSecond() { return $this->s; } public function toString() { $duration = 'P'; if ($this->y > 0) { $duration.= $this->y . 'Y'; } if ($this->m > 0) { $duration.= $this->m . 'M'; } if ($this->d > 0) { $duration.= $this->d . 'D'; } if ($this->h > 0 || $this->i > 0 || $this->s > 0) { $duration.= 'T'; if ($this->h > 0) { $duration.= $this->h . 'H'; } if ($this->i > 0) { $duration.= $this->i . 'M'; } if ($this->s > 0) { $duration.= $this->s . 'S'; } } return $duration; } public function __toString() { return $this->toString(); } public function jsonSerialize() { return $this->toString(); } protected function validate($duration) { $duration = (string) $duration; $result = preg_match('/^' . self::getPattern() . '$/', $duration); if (!$result) { throw new InvalidArgumentException('Must be duration format'); } return $duration; } public static function fromDateInterval(DateInterval $interval) { return new self($interval->y, $interval->m, $interval->d, $interval->h, $interval->i, $interval->s); } /** * Returns the seconds of an DateInterval recalculating years, months etc. * * @param DateInterval $interval * @return integer */ public static function getSecondsFromInterval(DateInterval $interval) { $map = [31536000, 2592000, 86400, 3600, 60, 1]; $parts = explode('.', $interval->format('%y.%m.%d.%h.%i.%s')); $value = 0; foreach ($parts as $key => $val) { $value+= $val * $map[$key]; } return $value; } /** * @see http://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#duration-lexical-space */ public static function getPattern() { return '-?P((([0-9]+Y([0-9]+M)?([0-9]+D)?|([0-9]+M)([0-9]+D)?|([0-9]+D))(T(([0-9]+H)([0-9]+M)?([0-9]+(\.[0-9]+)?S)?|([0-9]+M)([0-9]+(\.[0-9]+)?S)?|([0-9]+(\.[0-9]+)?S)))?)|(T(([0-9]+H)([0-9]+M)?([0-9]+(\.[0-9]+)?S)?|([0-9]+M)([0-9]+(\.[0-9]+)?S)?|([0-9]+(\.[0-9]+)?S))))'; } } __halt_compiler();----SIGNATURE:----arNm8DQD/mJ/Whk9kPomT+AqQ2cza2KRNvUFEQtQHbzVVRvu0ECXsHlt0TqJVvloAfXKDx9iDU3oKmPgA0h4mkCKjby/cQFjwVzNnHFpEzu1MHXYpKvrQJZ+b9iFkMZIlZc7ajl/JMT2e2Kryslp0AcQRooiOsRXgGsyj70+gmnQ9wuM/PsbC/NTzZSDCmKKGe0mYcBzJuJ0xWzXngaY7LzKhK+zOJ6b+PfRegqjp0ull2eJMv1+D2w8BgvB+bjvalL20i77X85FKi8Ce1P/lUGgvLEWfmhnGeaSzU5f9kkGfZhx8tm/jCp00qn6eqLUiTkFJE3UtBjfOw0M0LGJVL2FMPeWAleD5r+JDmns1sQX5pLhhyqsRwyDpuOH9wa79IBdpPXATiOmfPaYF+fvjKfasHLRuzHkXBiWiX7RA/59wxjkybEeB/y464wSD460GxfU7KBXubqBr6gPHm+ZhqDDRoF8oZWM8EA18JSXdRkVTh8fMIX+O1Rdmavn3cSOe2VDqGpLn3XRra4GwqKkyh7rk7LDoOJdx+1wcxrxE/ZXGD8KMWkpYqxl0mb0ZY31rst1VOX3qPbWB67Po4kIKz86n4MnLvftFzGAU9XmwMVPD2M+gJXsIJhaT4UHgRRdWsIgGlYu/4KUteBRAhfHqzXCcEzX4habXwXQqjGFdVY=----ATTACHMENT:----NjU0MTQ5NDQ0NDI1NjUzNyA2MjQ1OTEzMTExMTY5MjEzIDUxNjM4MzEzNzAxMjU0MTg=