PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezlocale/classes/ezdate.php

https://github.com/Yannix/ezpublish
PHP | 317 lines | 171 code | 26 blank | 120 comment | 33 complexity | 2523f1a23a1f13184da23e6e13ec915c MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZDate class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZDate ezdate.php
  12. \ingroup eZLocale
  13. \brief Locale aware date handler
  14. eZDate handles date values in months, days and years.
  15. The time stored as a timestamp with 0 hours, 0 minutes and 0 seconds.
  16. A new instance of eZDate will automaticly use the current locale and current date,
  17. if you however want a different locale use the setLocale() function. The current locale can be
  18. fetched with locale().
  19. Change the date directly with setYear(), setMonth(), setDay() and setMDY().
  20. You can also adjust the date relative to it's current value by using adjustDate().
  21. Use timeStamp() to get the current timestamp value or year(), month() and day()
  22. for the respective values.
  23. When creating new times you're advised to use the static create()
  24. function which returns a new eZDate object. You can also create a copy
  25. with the duplicate() function.
  26. Date checking is done with the isGreaterThan() and isEqualTo() functions.
  27. Text output is done with toString() which can return a long string (default) or
  28. short string representation according to the current locale.
  29. Example:
  30. \code
  31. $us_locale = eZLocale::instance( 'us' );
  32. $date1 = new eZDate();
  33. $date2 = eZDate::create();
  34. $date2->setLocale( $us_locale );
  35. $date2->adjustDate( 1, 2, 3 );
  36. $date3 = $date1->duplicate();
  37. print( $date1->toString() );
  38. print( $date2->toString( true ) );
  39. print( $date1->isEqualTo( $date3 ) ? 'true' : 'false' ); // Prints 'true'
  40. \endcode
  41. \sa eZTime, eZDateTime, eZLocale
  42. */
  43. class eZDate
  44. {
  45. /*!
  46. Creates a new date object with default locale, if $date is not supplied
  47. the current date is used.
  48. */
  49. function eZDate( $date = false )
  50. {
  51. if ( $date === false )
  52. {
  53. $date = mktime( 0, 0, 0 );
  54. }
  55. else
  56. {
  57. $arr = getdate( $date );
  58. $date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
  59. }
  60. $this->Date = $date;
  61. $this->Locale = eZLocale::instance();
  62. $this->IsValid = $date > 0;
  63. }
  64. function attributes()
  65. {
  66. return array( 'timestamp',
  67. 'is_valid',
  68. 'year',
  69. 'month',
  70. 'day' );
  71. }
  72. function hasAttribute( $name )
  73. {
  74. return in_array( $name, $this->attributes() );
  75. }
  76. function attribute( $name )
  77. {
  78. if ( $name == 'timestamp' )
  79. return $this->timeStamp();
  80. else if ( $name == 'is_valid' )
  81. return $this->isValid();
  82. else if ( $name == 'day' )
  83. return $this->day();
  84. else if ( $name == 'year' )
  85. return $this->year();
  86. else if ( $name == 'month' )
  87. return $this->month();
  88. eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
  89. return false;
  90. }
  91. /*!
  92. \return true if the date has valid data.
  93. */
  94. function isValid()
  95. {
  96. return $this->IsValid;
  97. }
  98. /*!
  99. Sets the locale to $locale which is used in text output.
  100. */
  101. function setLocale( $locale )
  102. {
  103. $this->Locale = $locale;
  104. }
  105. /*!
  106. Returns the current locale.
  107. */
  108. function locale()
  109. {
  110. return $this->Locale;
  111. }
  112. /*!
  113. Returns the timestamp value, this is the number of seconds since the epoch
  114. with hours, minutes and seconds set to 0.
  115. \note The value is returned as a reference and should not be modified.
  116. */
  117. function timeStamp()
  118. {
  119. return $this->Date;
  120. }
  121. function setTimeStamp( $stamp )
  122. {
  123. $this->Date = $stamp;
  124. $this->IsValid = $stamp > 0;
  125. }
  126. /*!
  127. Sets the year leaving the other elements untouched.
  128. */
  129. function setYear( $year )
  130. {
  131. $arr = getdate( $this->Date );
  132. $this->Date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $year );
  133. }
  134. /*!
  135. Sets the month leaving the other elements untouched.
  136. */
  137. function setMonth( $month )
  138. {
  139. $arr = getdate( $this->Date );
  140. $this->Date = mktime( 0, 0, 0, $month, $arr['mday'], $arr['year'] );
  141. }
  142. /*!
  143. Sets the day leaving the other elements untouched.
  144. */
  145. function setDay( $day )
  146. {
  147. $arr = getdate( $this->Date );
  148. $this->Date = mktime( 0, 0, 0, $arr['mon'], $day, $arr['year'] );
  149. }
  150. /*!
  151. Returns the year element.
  152. */
  153. function year()
  154. {
  155. return date( 'Y', $this->Date );
  156. }
  157. /*!
  158. Returns the month element.
  159. */
  160. function month()
  161. {
  162. return date( 'm', $this->Date );
  163. }
  164. /*!
  165. Returns the day element.
  166. */
  167. function day()
  168. {
  169. return date( 'd', $this->Date );
  170. }
  171. /*!
  172. Sets the year, month and day elements. If $day or $year is omitted or set 0
  173. they will get a value taken from the current time.
  174. */
  175. function setMDY( $month, $day = 0, $year = 0 )
  176. {
  177. if ( $year != 0 )
  178. $date = mktime( 0, 0, 0, $month, $day, $year );
  179. else if ( $day != 0 )
  180. $date = mktime( 0, 0, 0, $month, $day );
  181. else
  182. $date = mktime( 0, 0, 0, $month );
  183. $this->Date = $date;
  184. }
  185. /*!
  186. Adjusts the date relative to it's current value. This is useful for adding/subtracting
  187. years, months or days to an existing date.
  188. */
  189. function adjustDate( $month, $day = 0, $year = 0 )
  190. {
  191. $arr = getdate( $this->Date );
  192. $date = mktime( 0, 0, 0, $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
  193. $this->Date = $date;
  194. }
  195. /*!
  196. Returns true if this object has a date greater than $date. $date can be specified as
  197. a timestamp value or as an eZDate object. If $equal is true it returns true if
  198. they are equal as well.
  199. */
  200. function isGreaterThan( $date, $equal = false )
  201. {
  202. $d1 = $this->timeStamp();
  203. if ( $date instanceof eZDate )
  204. {
  205. $d2 = $date->timeStamp();
  206. }
  207. else
  208. {
  209. $arr = getdate( $date );
  210. $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
  211. }
  212. if ( $d1 > $d2 )
  213. return true;
  214. else if ( $equal and $d1 == $d2 )
  215. return true;
  216. else
  217. return false;
  218. }
  219. /*!
  220. Returns true if this object is equal to $date. $date can be specified as
  221. a timestamp value or as an eZDate object.
  222. */
  223. function isEqualTo( $date )
  224. {
  225. $d1 = $this->timeStamp();
  226. if ( $date instanceof eZDate )
  227. {
  228. $d2 = $date->timeStamp();
  229. }
  230. else
  231. {
  232. $arr = getdate( $date );
  233. $d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
  234. }
  235. return $d1 == $d2;
  236. }
  237. /*!
  238. Creates a new eZDate object with the date values $month, $day and $year and returns a reference to it.
  239. Any value can be ommitted or set to 0 to use the current date value.
  240. */
  241. static function create( $month, $day = 0, $year = 0 )
  242. {
  243. if ( $year != 0 )
  244. $date = mktime( 0, 0, 0, $month, $day, $year );
  245. else if ( $day != 0 )
  246. $date = mktime( 0, 0, 0, $month, $day );
  247. else
  248. $date = mktime( 0, 0, 0, $month );
  249. $newDateObject = new eZDate( $date );
  250. return $newDateObject;
  251. }
  252. /*!
  253. \deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
  254. Creates an exact copy of this object and returns it.
  255. */
  256. function duplicate()
  257. {
  258. $copy = clone $this;
  259. return $copy;
  260. }
  261. /*!
  262. Creates a string representation of the date using the current locale and returns it.
  263. If $short is true a short representation is used.
  264. */
  265. function toString( $short = false )
  266. {
  267. if ( $short )
  268. $str = $this->Locale->formatShortDate( $this->Date );
  269. else
  270. $str = $this->Locale->formatDate( $this->Date );
  271. return $str;
  272. }
  273. /// Locale object, is just a reference to minimize memory usage.
  274. public $Locale;
  275. /// The current date as a timestamp without hour, minute or second values
  276. public $Date;
  277. public $IsValid;
  278. }
  279. ?>