DateTime.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * An extension of PHP's DateTime class to provide dirty flagging and easier formatting options.
  8. *
  9. * All date and datetime fields from your database will be created as instances of this class.
  10. *
  11. * Example of formatting and changing the default format:
  12. *
  13. * <code>
  14. * $now = new ActiveRecord\DateTime('2010-01-02 03:04:05');
  15. * ActiveRecord\DateTime::$DEFAULT_FORMAT = 'short';
  16. *
  17. * echo $now->format(); # 02 Jan 03:04
  18. * echo $now->format('atom'); # 2010-01-02T03:04:05-05:00
  19. * echo $now->format('Y-m-d'); # 2010-01-02
  20. *
  21. * # __toString() uses the default formatter
  22. * echo (string)$now; # 02 Jan 03:04
  23. * </code>
  24. *
  25. * You can also add your own pre-defined friendly formatters:
  26. *
  27. * <code>
  28. * ActiveRecord\DateTime::$FORMATS['awesome_format'] = 'H:i:s m/d/Y';
  29. * echo $now->format('awesome_format') # 03:04:05 01/02/2010
  30. * </code>
  31. *
  32. * @package ActiveRecord
  33. * @see http://php.net/manual/en/class.datetime.php
  34. */
  35. class DateTime extends \DateTime
  36. {
  37. /**
  38. * Default format used for format() and __toString()
  39. */
  40. public static $DEFAULT_FORMAT = 'rfc2822';
  41. /**
  42. * Pre-defined format strings.
  43. */
  44. public static $FORMATS = array(
  45. 'db' => 'Y-m-d H:i:s',
  46. 'number' => 'YmdHis',
  47. 'time' => 'H:i',
  48. 'short' => 'd M H:i',
  49. 'long' => 'F d, Y H:i',
  50. 'atom' => \DateTime::ATOM,
  51. 'cookie' => \DateTime::COOKIE,
  52. 'iso8601' => \DateTime::ISO8601,
  53. 'rfc822' => \DateTime::RFC822,
  54. 'rfc850' => \DateTime::RFC850,
  55. 'rfc1036' => \DateTime::RFC1036,
  56. 'rfc1123' => \DateTime::RFC1123,
  57. 'rfc2822' => \DateTime::RFC2822,
  58. 'rfc3339' => \DateTime::RFC3339,
  59. 'rss' => \DateTime::RSS,
  60. 'w3c' => \DateTime::W3C);
  61. private $model;
  62. private $attribute_name;
  63. public function attribute_of($model, $attribute_name)
  64. {
  65. $this->model = $model;
  66. $this->attribute_name = $attribute_name;
  67. }
  68. /**
  69. * Formats the DateTime to the specified format.
  70. *
  71. * <code>
  72. * $datetime->format(); # uses the format defined in DateTime::$DEFAULT_FORMAT
  73. * $datetime->format('short'); # d M H:i
  74. * $datetime->format('Y-m-d'); # Y-m-d
  75. * </code>
  76. *
  77. * @see FORMATS
  78. * @see get_format
  79. * @param string $format A format string accepted by get_format()
  80. * @return string formatted date and time string
  81. */
  82. public function format($format=null)
  83. {
  84. return parent::format(self::get_format($format));
  85. }
  86. /**
  87. * Returns the format string.
  88. *
  89. * If $format is a pre-defined format in $FORMATS it will return that otherwise
  90. * it will assume $format is a format string itself.
  91. *
  92. * @see FORMATS
  93. * @param string $format A pre-defined string format or a raw format string
  94. * @return string a format string
  95. */
  96. public static function get_format($format=null)
  97. {
  98. // use default format if no format specified
  99. if (!$format)
  100. $format = self::$DEFAULT_FORMAT;
  101. // format is a friendly
  102. if (array_key_exists($format, self::$FORMATS))
  103. return self::$FORMATS[$format];
  104. // raw format
  105. return $format;
  106. }
  107. public function __toString()
  108. {
  109. return $this->format();
  110. }
  111. private function flag_dirty()
  112. {
  113. if ($this->model)
  114. $this->model->flag_dirty($this->attribute_name);
  115. }
  116. public function setDate($year, $month, $day)
  117. {
  118. $this->flag_dirty();
  119. call_user_func_array(array($this,'parent::setDate'),func_get_args());
  120. }
  121. public function setISODate($year, $week , $day=null)
  122. {
  123. $this->flag_dirty();
  124. call_user_func_array(array($this,'parent::setISODate'),func_get_args());
  125. }
  126. public function setTime($hour, $minute, $second=null)
  127. {
  128. $this->flag_dirty();
  129. call_user_func_array(array($this,'parent::setTime'),func_get_args());
  130. }
  131. public function setTimestamp($unixtimestamp)
  132. {
  133. $this->flag_dirty();
  134. call_user_func_array(array($this,'parent::setTimestamp'),func_get_args());
  135. }
  136. }
  137. ?>