PropelDateTime.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * DateTime subclass which supports serialization.
  11. *
  12. * Currently Propel is not using this for storing date/time objects
  13. * within model objeects; however, we are keeping it in the repository
  14. * because it is useful if you want to store a DateTime object in a session.
  15. *
  16. * @author Alan Pinstein
  17. * @author Soenke Ruempler
  18. * @author Hans Lellelid
  19. * @package propel.runtime.util
  20. */
  21. class PropelDateTime extends DateTime
  22. {
  23. /**
  24. * A string representation of the date, for serialization.
  25. * @var string
  26. */
  27. private $dateString;
  28. /**
  29. * A string representation of the time zone, for serialization.
  30. * @var string
  31. */
  32. private $tzString;
  33. /**
  34. * Convenience method to enable a more fluent API.
  35. * @param string $date Date/time value.
  36. * @param DateTimeZone $tz (optional) timezone
  37. */
  38. public static function newInstance($date, DateTimeZone $tz = null)
  39. {
  40. if ($tz) {
  41. return new DateTime($date, $tz);
  42. } else {
  43. return new DateTime($date);
  44. }
  45. }
  46. /**
  47. * PHP "magic" function called when object is serialized.
  48. * Sets an internal property with the date string and returns properties
  49. * of class that should be serialized.
  50. * @return array string[]
  51. */
  52. function __sleep()
  53. {
  54. // We need to use a string without a time zone, due to
  55. // PHP bug: http://bugs.php.net/bug.php?id=40743
  56. $this->dateString = $this->format('Y-m-d H:i:s');
  57. $this->tzString = $this->getTimeZone()->getName();
  58. return array('dateString', 'tzString');
  59. }
  60. /**
  61. * PHP "magic" function called when object is restored from serialized state.
  62. * Calls DateTime constructor with previously stored string value of date.
  63. */
  64. function __wakeup()
  65. {
  66. parent::__construct($this->dateString, new DateTimeZone($this->tzString));
  67. }
  68. }