PageRenderTime 29ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/i18n_v2/inc/class.I18NformatDate.inc.php

http://flaimo-php.googlecode.com/
PHP | 410 lines | 220 code | 35 blank | 155 comment | 5 complexity | 7d7af5b91f5e9c2460391794d60c7569 MD5 | raw file
  1. <?php
  2. /**
  3. * load base class which takes care of all the other includes via it's autoload function
  4. */
  5. require_once 'class.I18Nbase.inc.php';
  6. /**
  7. * formats dates and times based on the current locale
  8. * @author Michael Wimmer <flaimo@gmail.com>
  9. * @category flaimo-php
  10. * @example ../www_root/i18n_example_script.php i18n example script
  11. * @license GNU General Public License v3
  12. * @link http://code.google.com/p/flaimo-php/
  13. * @package i18n
  14. * @version 2.3.1
  15. */
  16. class I18NformatDate extends I18Nbase {
  17. /**
  18. * @var string namespaces needed for translating strings in this class
  19. */
  20. protected $namespace = 'lang_classFormatDate';
  21. /**
  22. * @var object holds a I18Ntranslator object
  23. */
  24. protected $translator;
  25. /**
  26. * @var array
  27. */
  28. protected $display_times;
  29. /**
  30. * @var int
  31. */
  32. protected $current_timeformat;
  33. /**#@+
  34. * @var string Format definitions for the different display formats
  35. */
  36. protected $short_date;
  37. protected $short_time;
  38. protected $short_datetime;
  39. protected $middle_date;
  40. protected $middle_time;
  41. protected $middle_datetime;
  42. protected $long_date;
  43. protected $long_time;
  44. protected $long_datetime;
  45. /**#@-*/
  46. /**
  47. * @var array
  48. */
  49. protected $month_array = array('january','february','march','april','may',
  50. 'june','july','august','september','october',
  51. 'november','december');
  52. /**
  53. * @var array
  54. */
  55. protected $day_array = array('sunday','monday','tuesday','wednesday',
  56. 'thursday','friday','saturday');
  57. /**
  58. * @param object $locale I18Nlocale
  59. * @return void
  60. * @uses I18Nbase::getI18NfactoryLocale()
  61. * @uses I18Ntranslator
  62. * @uses I18NformatDate::$translator
  63. * @uses I18NformatDate::readSettings()
  64. * @uses I18Nbase::getI18Nuser()
  65. * @uses I18NformatDate::$current_timeformat
  66. * @uses I18Nbase::getI18Nsetting()
  67. * @uses I18Nbase::__construct()
  68. */
  69. public function __construct(&$locale = NULL) {
  70. if (!($locale instanceOf I18Nlocale)) {
  71. $locale =& parent::getI18NfactoryLocale();
  72. } // end if
  73. $this->translator = new I18Ntranslator($this->namespace, $locale);
  74. $this->readSettings();
  75. $user_pref =& parent::getI18Nuser()->getPrefTimeFormat();
  76. $this->current_timeformat = ($user_pref != FALSE) ? $user_pref : parent::getI18Nsetting('default_timeset');
  77. parent::__construct();
  78. } // end constructor
  79. /**
  80. * fetches the needed l10n settings into class vars
  81. * @uses I18NformatDate::$translator
  82. * @return void
  83. */
  84. protected function readSettings() {
  85. $this->short_date =& $this->translator->getTranslatorLocale()->getL10Nsetting('short_date');
  86. $this->short_time =& $this->translator->getTranslatorLocale()->getL10Nsetting('short_time');
  87. $this->short_datetime =& $this->translator->getTranslatorLocale()->getL10Nsetting('short_datetime');
  88. $this->middle_date =& $this->translator->getTranslatorLocale()->getL10Nsetting('middle_date');
  89. $this->middle_time =& $this->translator->getTranslatorLocale()->getL10Nsetting('middle_time');
  90. $this->middle_datetime =& $this->translator->getTranslatorLocale()->getL10Nsetting('middle_datetime');
  91. $this->long_date =& $this->translator->getTranslatorLocale()->getL10Nsetting('long_date');
  92. $this->long_time =& $this->translator->getTranslatorLocale()->getL10Nsetting('long_time');
  93. $this->long_datetime =& $this->translator->getTranslatorLocale()->getL10Nsetting('long_datetime');
  94. } // end function
  95. /**
  96. * Checks if a given timecode is valid
  97. * @param int $code
  98. * @return boolean
  99. */
  100. public static function isValidTimeCode($code = 0) {
  101. return (boolean) ($code < 0 || $code > 2) ? FALSE : TRUE;
  102. } // end function
  103. /**#@+
  104. * Checks if a string is a valid iso date
  105. * @param string $date
  106. * @return boolean
  107. */
  108. public static function isValidISODate($date) {
  109. return (boolean) ((preg_match('(^\d{4}-\d{2}-\d{2}$)', $date) > 0) ? TRUE : FALSE);
  110. } // end function
  111. public static function isValidISODateTime($date) {
  112. return (boolean) ((preg_match('(^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$)', $date) > 0) ? TRUE : FALSE);
  113. } // end function
  114. /**#@-*/
  115. /**
  116. * Checks if a timestamp is valid
  117. * @param int $timestamp
  118. * @return boolean
  119. */
  120. public static function isValidUnixTimeStamp($timestamp) {
  121. return (boolean) ((preg_match('(^\d{1,10}$)', $timestamp) > 0) ? TRUE : FALSE);
  122. } // end function
  123. /**#@+
  124. * converts iso to timestamp
  125. * @param string $date
  126. * @return int
  127. */
  128. public static function ISOdateToUnixtimestamp($date = '1900-01-01') {
  129. return strtotime($date);
  130. } // end function
  131. public static function ISOdatetimeToUnixtimestamp($date = '1900-01-01 00:00:00') {
  132. return strtotime($date);
  133. } // end function
  134. /**#@-*/
  135. /**#@+
  136. * converts timestamp to iso date
  137. * @param int $timestamp
  138. * @return string
  139. */
  140. public static function unixTimestampToISOdate($timestamp = 0) {
  141. return date('Y-m-d', $timestamp);
  142. } // end function
  143. public static function unixTimestampToISOtime($timestamp = 0) {
  144. return date('H:i:s', $timestamp);
  145. } // end function
  146. public static function unixTimestampToISOdatetime($timestamp = 0) {
  147. return date('Y-m-d H:i:s', $timestamp);
  148. } // end function
  149. /**#@-*/
  150. /**#@+
  151. * @param int $timestamp
  152. * @return string
  153. * @uses I18NformatDate::$translator
  154. */
  155. /**
  156. * return a translation of a monthname based on the current translator locale
  157. */
  158. protected function monthName($timestamp = 0) {
  159. $month = (int) date('n', $timestamp) - 1;
  160. return $this->translator->_($this->month_array[$month], 'lang_classFormatDate');
  161. } // end function
  162. /**
  163. * return a translation of a dayname based on the current translator locale
  164. */
  165. protected function dayName($timestamp = 0) {
  166. $day = (int) date('w', $timestamp);
  167. return $this->translator->_($this->day_array[$day], 'lang_classFormatDate');
  168. } // end function
  169. /**#@-*/
  170. /**
  171. * returns an "/" encoded string
  172. * @param string $string
  173. * @return string $newstring
  174. */
  175. protected static function encodeDateStrings($string = '') {
  176. // maybe has to be rewritten for multibyte...
  177. $length = (int) strlen($string);
  178. $newstring = '';
  179. for ($i = 0; $i < $length; $i++) {
  180. $newstring .= '\\' . $string[$i];
  181. } // end for
  182. return $newstring;
  183. } // end function
  184. /**
  185. * Returns correctly formated string for using with a date function
  186. * @param string $format how the timestring should be formated (raw from ini file)
  187. * @param int $timestamp
  188. * @uses I18Nbase::isFilledString()
  189. * @uses I18NformatDate::encodeDateStrings()
  190. * @uses I18NformatDate::dayName()
  191. * @uses I18NformatDate::monthName()
  192. * @uses I18NformatDate::$translator
  193. * @return string $format
  194. */
  195. protected function dateFilter($format = '', $timestamp = 0) {
  196. /* mb functions replace "str_replace function */
  197. if (parent::isFilledString($format) === FALSE) {
  198. return '';
  199. } elseif (!preg_match('(monthname|dayname|hour)', $format)) {
  200. return $format;
  201. } // end if
  202. $format = mb_eregi_replace('monthname_short', $this->encodeDateStrings(mb_substr($this->monthName($timestamp), 0, 3)), $format);
  203. switch($this->translator->getTranslatorLocale()->getI18Nlocale()) {
  204. case 'ru':
  205. default:
  206. $end = (int) 2;
  207. break;
  208. } // end switch
  209. $format = mb_eregi_replace('dayname_short', $this->encodeDateStrings(mb_substr($this->dayName($timestamp), 0, $end)), $format);
  210. $format = mb_eregi_replace('dayname', $this->encodeDateStrings($this->dayName($timestamp)), $format);
  211. $format = mb_eregi_replace('monthname', $this->encodeDateStrings($this->monthName($timestamp)), $format);
  212. $format = mb_eregi_replace('hour', $this->encodeDateStrings($this->translator->_('hour', 'lang_classFormatDate')), $format);
  213. return $format;
  214. } // end function
  215. /**#@+
  216. * Returns a formated timestamp
  217. * @param int $timestamp
  218. * @param string $format how the timestring should be formated
  219. * @uses I18NformatDate::swatchDate()
  220. * @uses I18NformatDate::unixTimestampToISOdate()
  221. * @uses I18NformatDate::dateFilter()
  222. * @uses I18NformatDate::$current_timeformat
  223. * @return string
  224. */
  225. protected function fDate($timestamp = 0, $format = '') {
  226. switch ($this->current_timeformat) {
  227. case 1: // swatch date
  228. return $this->swatchDate($timestamp);
  229. break;
  230. case 2: // iso date
  231. return $this->unixTimestampToISOdate($timestamp);
  232. break;
  233. default:
  234. return date($this->dateFilter($format, $timestamp), $timestamp);
  235. break;
  236. } // end switch
  237. } // end function
  238. protected function fTime($timestamp = 0, $format = '') {
  239. switch ($this->current_timeformat) {
  240. case 1: // swatch date
  241. return $this->swatchTime($timestamp);
  242. break;
  243. case 2: // iso date ;
  244. return $this->unixTimestampToISOtime($timestamp);
  245. break;
  246. default:
  247. return date($this->dateFilter($format, $timestamp), $timestamp);
  248. break;
  249. } // end switch
  250. } // end function
  251. protected function fDateTime($timestamp = 0, $format = '') {
  252. switch ($this->current_timeformat) {
  253. case 1: // swatch date
  254. return $this->swatchDate($timestamp) . ' รข&#x20AC;&#x201C; ' . $this->swatchTime($timestamp);
  255. break;
  256. case 2: // iso date
  257. return $this->unixTimestampToISOdatetime($timestamp);
  258. break;
  259. default:
  260. return date($this->dateFilter($format, $timestamp), $timestamp);
  261. break;
  262. } // end switch
  263. } // end function
  264. /**#@-*/
  265. /**#@+
  266. * returns a formated timestamp
  267. * @param int $timestamp
  268. * @return string formated date
  269. */
  270. /**
  271. * @uses fDate()
  272. */
  273. public function shortDate($timestamp = 0) {
  274. return $this->fDate($timestamp, $this->short_date);
  275. } // end function
  276. /**
  277. * @uses fDate()
  278. */
  279. public function middleDate($timestamp = 0) {
  280. return $this->fDate($timestamp, $this->middle_date);
  281. } // end function
  282. /**
  283. * @uses fDate()
  284. */
  285. public function longDate($timestamp = 0) {
  286. return $this->fDate($timestamp, $this->long_date);
  287. } // end function
  288. /**
  289. * @uses fTime()
  290. */
  291. public function shortTime($timestamp = 0) {
  292. return $this->fTime($timestamp, $this->short_time);
  293. } // end function
  294. /**
  295. * @uses fTime()
  296. */
  297. public function middleTime($timestamp = 0) {
  298. return $this->fTime($timestamp, $this->middle_time);
  299. } // end function
  300. /**
  301. * @uses fTime()
  302. */
  303. public function longTime($timestamp = 0) {
  304. return $this->fTime($timestamp, $this->long_time);
  305. } // end function
  306. /**
  307. * @uses fDateTime()
  308. */
  309. public function shortDateTime($timestamp = 0) {
  310. return $this->fDateTime($timestamp, $this->short_datetime);
  311. } // end function
  312. /**
  313. * @uses fDateTime()
  314. */
  315. public function middleDateTime($timestamp = 0) {
  316. return $this->fDateTime($timestamp, $this->middle_datetime);
  317. } // end function
  318. /**
  319. * @uses fDateTime()
  320. */
  321. public function longDateTime($timestamp = 0) {
  322. return $this->fDateTime($timestamp, $this->long_datetime);
  323. } // end function
  324. /**
  325. * returns swatch time
  326. * @param int $timestamp
  327. * @return string swatch time
  328. * @see swatchDate()
  329. * @author http://www.ypass.net/crap/internettime/ <http://www.ypass.net/crap/internettime/>
  330. */
  331. public static function swatchTime($timestamp = 0) {
  332. $rawbeat = split('[.]',(($timestamp % 86400) / 86.4), 2);
  333. return '@' . sprintf('%03d', $rawbeat[0]) . '://' . substr($rawbeat[1], 0, 2);
  334. } // end function
  335. /**
  336. * returns swatch date
  337. * @param int $timestamp
  338. * @return string swatch date
  339. * @see swatchTime()
  340. */
  341. public static function swatchDate($timestamp = 0) {
  342. return '@d' . date('d.m.y', $timestamp);
  343. } // end function
  344. /**
  345. * returns an array with possible time formats
  346. * @return array $display_times
  347. * @uses I18NformatDate::translator()
  348. */
  349. public function getPossibleTimeFormats() {
  350. if (!isset($this->display_times)) {
  351. $this->display_times = array($this->translator->_('standard_time'),
  352. $this->translator->_('swatch_time'),
  353. $this->translator->_('ISO_time'));
  354. } // end if
  355. return $this->display_times;
  356. } // end function
  357. /**
  358. * returns current time format as a string
  359. * @return string
  360. * @uses I18NformatDate::getPossibleTimeFormats()
  361. * @uses I18NformatDate::$current_timeformat
  362. */
  363. public function &getTimeFormat() {
  364. $times =& $this->getPossibleTimeFormats();
  365. return $times[$this->current_timeformat];
  366. } // end function
  367. } // end class I18NformatDate
  368. ?>