PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/system/classes/habaridatetime.php

https://github.com/psaintlaurent/Habari
PHP | 353 lines | 158 code | 39 blank | 156 comment | 10 complexity | 2ebe65b9be81a277d057bb099fecb3c2 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. * HabariDateTime class to wrap dates in.
  8. *
  9. * @property-read HabariDateTime $clone Returns a clonned object.
  10. * @property-read string $sql Returns a unix timestamp for inserting into DB.
  11. * @property-read int $int Returns a unix timestamp as integer.
  12. */
  13. class HabariDateTime extends DateTime
  14. {
  15. private static $default_timezone;
  16. private static $default_datetime_format = 'c';
  17. private static $default_date_format;
  18. private static $default_time_format;
  19. /**
  20. * Set default timezone to system default on init.
  21. *
  22. * @static
  23. */
  24. public static function __static()
  25. {
  26. if ( Options::get( 'timezone' ) ) {
  27. self::set_default_timezone( Options::get( 'timezone' ) );
  28. }
  29. self::$default_timezone = date_default_timezone_get();
  30. self::$default_date_format = Options::get('dateformat');
  31. self::$default_time_format = Options::get('timeformat');
  32. if ( self::$default_date_format || self::$default_time_format ) {
  33. self::set_default_datetime_format( self::$default_date_format . ' ' . self::$default_time_format );
  34. }
  35. }
  36. /**
  37. * Set default date/time format. The format is the same as the
  38. * internal php {@link http://ca.php.net/date date() function}.
  39. *
  40. * @static
  41. * @param string $format The date format.
  42. */
  43. public static function set_default_datetime_format( $format )
  44. {
  45. self::$default_datetime_format = $format;
  46. }
  47. /**
  48. * Get the default date/time format set.
  49. *
  50. * @static
  51. * @see set_default_datetime_format()
  52. * @return string The date format set.
  53. */
  54. public static function get_default_datetime_format()
  55. {
  56. return self::$default_datetime_format;
  57. }
  58. /**
  59. * Sets the timezone for Habari and PHP.
  60. *
  61. * @static
  62. * @param string $timezone A timezone name, not an abbreviation, for example 'America/New York'
  63. */
  64. public static function set_default_timezone( $timezone )
  65. {
  66. self::$default_timezone = $timezone;
  67. date_default_timezone_set( self::$default_timezone );
  68. }
  69. /**
  70. * Get the timezone for Habari and PHP.
  71. * Defaults to system timezone if not set.
  72. *
  73. * @static
  74. * @see set_default_timezone()
  75. * @param string The deafult timezone.
  76. */
  77. public static function get_default_timezone()
  78. {
  79. return self::$default_timezone;
  80. }
  81. /**
  82. * Helper function to create a HabariDateTime object for the given
  83. * time and timezone. If no time is given, defaults to 'now'. If no
  84. * timezone given defaults to timezone set in {@link set_default_timezone()}
  85. *
  86. * @static
  87. * @see DateTime::__construct()
  88. * @param string $time String in a format accepted by
  89. * {@link http://ca.php.net/strtotime strtotime()}, defaults to "now".
  90. * @param string $timezone A timezone name, not an abbreviation.
  91. */
  92. public static function date_create( $time = null, $timezone = null )
  93. {
  94. if ( $time instanceOf HabariDateTime ) {
  95. return $time;
  96. }
  97. elseif ( $time instanceOf DateTime ) {
  98. $time = $time->format('U');
  99. }
  100. elseif ( $time == null ) {
  101. $time = 'now';
  102. }
  103. elseif ( is_numeric($time) ) {
  104. $time = '@' . $time;
  105. }
  106. if ( $timezone === null ) {
  107. $timezone = self::$default_timezone;
  108. }
  109. // passing the timezone to construct doesn't seem to do anything.
  110. $datetime = new HabariDateTime($time);
  111. $datetime->set_timezone($timezone);
  112. return $datetime;
  113. }
  114. /**
  115. * Set the date of this object
  116. *
  117. * @see DateTime::setDate()
  118. * @param int $year Year of the date
  119. * @param int $month Month of the date
  120. * @param int $day Day of the date
  121. */
  122. public function set_date( $year, $month, $day )
  123. {
  124. parent::setDate($year, $month, $day);
  125. return $this;
  126. }
  127. /**
  128. * Sets the ISO date
  129. *
  130. * @see DateTime::setISODate()
  131. * @param int $year Year of the date
  132. * @param int $month Month of the date
  133. * @param int $day Day of the date
  134. */
  135. public function set_isodate( $year, $week, $day = null )
  136. {
  137. parent::setISODate($year, $week, $day);
  138. return $this;
  139. }
  140. /**
  141. * Set the time of this object
  142. *
  143. * @see DateTime::setTime()
  144. * @param int $hour Hour of the time
  145. * @param int $minute Minute of the time
  146. * @param int $second Second of the time
  147. */
  148. public function set_time( $hour, $minute, $second = null )
  149. {
  150. parent::setTime($hour, $minute, $second);
  151. return $this;
  152. }
  153. /**
  154. * Set the timezone for this datetime object. Can be either string
  155. * timezone identifier, or DateTimeZone object.
  156. *
  157. * @see DateTime::setTimezone()
  158. * @param mixed The timezone to use.
  159. * @return HabariDateTime $this object.
  160. */
  161. public function set_timezone( $timezone )
  162. {
  163. if ( ! $timezone instanceof DateTimeZone ) {
  164. $timezone = new DateTimeZone($timezone);
  165. }
  166. parent::setTimezone($timezone);
  167. return $this;
  168. }
  169. /**
  170. * Get the timezone identifier that is set for this datetime object.
  171. *
  172. * @return DateTimeZone The timezone object.
  173. */
  174. public function get_timezone()
  175. {
  176. return parent::getTimezone();
  177. }
  178. /**
  179. * Returns date formatted according to given format.
  180. *
  181. * @see DateTime::format()
  182. * @param string $format Format accepted by {@link http://php.net/date date()}.
  183. * @return string The formatted date, false on failure.
  184. */
  185. public function format($format = null)
  186. {
  187. if ( $format === null ) {
  188. $format = self::$default_datetime_format;
  189. }
  190. return parent::format($format);
  191. }
  192. /**
  193. * Returns date components inserted into a string
  194. *
  195. * Example:
  196. * echo HabariDateTime::date_create('2010-01-01')->text_format('The year was {Y}.');
  197. * // Expected output: The year was 2010.
  198. *
  199. * @param string $format A string with single-character date format codes {@link http://php.net/date date()} surrounded by braces
  200. * @return string The string with date components inserted
  201. */
  202. public function text_format($format)
  203. {
  204. return preg_replace_callback('%\{(\w)\}%iu', array($this, 'text_format_callback'), $format);
  205. }
  206. /**
  207. * Callback method for supplying replacements for HabariDatTime::text_format()
  208. *
  209. * @param array $matches The matches found in the regular expression.
  210. * @return string The date component value for the matched character.
  211. */
  212. private function text_format_callback($matches)
  213. {
  214. return $this->format($matches[1]);
  215. }
  216. /**
  217. * Alters the timestamp
  218. *
  219. * @param string $format A format accepted by {@link http://php.net/strtotime strtotime()}.
  220. * @return HabariDateTime $this object.
  221. */
  222. public function modify( $args )
  223. {
  224. parent::modify( $args );
  225. return $this;
  226. }
  227. /**
  228. * @see format()
  229. */
  230. public function get($format = null)
  231. {
  232. return $this->format($format);
  233. }
  234. /**
  235. * Echos date formatted according to given format.
  236. *
  237. * @see format()
  238. * @param string $format Format accepted by {@link http://php.net/date date()}.
  239. */
  240. public function out($format = null)
  241. {
  242. echo $this->format($format);
  243. }
  244. /**
  245. * Magic method called when this object is cast to string. Returns the
  246. * unix timestamp of this object.
  247. *
  248. * @return string The unix timestamp
  249. */
  250. public function __toString()
  251. {
  252. return $this->format('U');
  253. }
  254. /**
  255. * Magic method to get magic ponies... properties, I mean.
  256. */
  257. public function __get($property)
  258. {
  259. switch ($property) {
  260. case 'clone':
  261. return clone $this;
  262. case 'sql':
  263. return $this->format('U');
  264. break;
  265. case 'int':
  266. return intval( $this->format('U') );
  267. break;
  268. case 'time':
  269. return $this->format( self::get_default_time_format() );
  270. break;
  271. case 'date':
  272. return $this->format( self::get_default_date_format() );
  273. break;
  274. default:
  275. $info = getdate($this->format('U'));
  276. $info['mon0'] = substr('0' . $info['mon'], -2, 2);
  277. $info['mday0'] = substr('0' . $info['mday'], -2, 2);
  278. if ( isset($info[$property]) ) {
  279. return $info[$property];
  280. }
  281. return $this->$property;
  282. }
  283. }
  284. /**
  285. * Return the default date format, as set in the Options table
  286. *
  287. * @return The default date format
  288. **/
  289. public static function get_default_date_format ( ) {
  290. return self::$default_date_format;
  291. }
  292. /**
  293. * Return the default time format, as set in the Options table
  294. *
  295. * @return The default time format
  296. **/
  297. public static function get_default_time_format ( ) {
  298. return self::$default_time_format;
  299. }
  300. /**
  301. * Returns an associative array containing the date information for
  302. * this HabariDateTime object, as per {@link http://php.net/getdate getdate()}
  303. *
  304. * @return array Associative array containing the date information
  305. */
  306. public function getdate()
  307. {
  308. $info = getdate($this->format('U'));
  309. $info['mon0'] = substr('0' . $info['mon'], -2, 2);
  310. $info['mday0'] = substr('0' . $info['mday'], -2, 2);
  311. return $info;
  312. }
  313. }
  314. ?>