DateTime.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage Value
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_XmlRpc_Value_Scalar
  24. */
  25. require_once 'Zend/XmlRpc/Value/Scalar.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_XmlRpc
  29. * @subpackage Value
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_XmlRpc_Value_DateTime extends Zend_XmlRpc_Value_Scalar
  34. {
  35. /**
  36. * Set the value of a dateTime.iso8601 native type
  37. *
  38. * The value is in iso8601 format, minus any timezone information or dashes
  39. *
  40. * @param mixed $value Integer of the unix timestamp or any string that can be parsed
  41. * to a unix timestamp using the PHP strtotime() function
  42. */
  43. public function __construct($value)
  44. {
  45. $this->_type = self::XMLRPC_TYPE_DATETIME;
  46. // If the value is not numeric, we try to convert it to a timestamp (using the strtotime function)
  47. if (is_numeric($value)) { // The value is numeric, we make sure it is an integer
  48. $timestamp = (int)$value;
  49. } else {
  50. $timestamp = strtotime($value);
  51. if ($timestamp === false || $timestamp == -1) { // cannot convert the value to a timestamp
  52. throw new Zend_XmlRpc_Value_Exception('Cannot convert given value \''. $value .'\' to a timestamp');
  53. }
  54. }
  55. $date = date('c', $timestamp); // Convert the timestamp to iso8601 format
  56. // Strip out TZ information and dashes
  57. $date = preg_replace('/(\+|-)\d{2}:\d{2}$/', '', $date);
  58. $date = str_replace('-', '', $date);
  59. $this->_value = $date;
  60. }
  61. /**
  62. * Return the value of this object as iso8601 dateTime value
  63. *
  64. * @return int As a Unix timestamp
  65. */
  66. public function getValue()
  67. {
  68. return $this->_value;
  69. }
  70. }