PageRenderTime 67ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rmcommon/trunk/class/timeformatter.php

http://bitcero-modules.googlecode.com/
PHP | 130 lines | 83 code | 23 blank | 24 comment | 5 complexity | 7369a1b1b388c4a0cf31e046a509947d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Id: timeformatter.php 1056 2012-09-12 15:43:20Z i.bitcero $
  3. // --------------------------------------------------------------
  4. // Red M?Šxico Common Utilities
  5. // A framework for Red M?Šxico Modules
  6. // Author: Eduardo Cort?Šs <i.bitcero@gmail.com>
  7. // Email: i.bitcero@gmail.com
  8. // License: GPL 2.0
  9. // --------------------------------------------------------------
  10. class RMTimeFormatter
  11. {
  12. private $time = 0;
  13. private $format = '';
  14. /**
  15. * Initialize this class
  16. *
  17. * @param int Unix timestamp
  18. * @param string Format for time (e.g. Created on %M% %d%, %Y%)
  19. * @return RMTimeFormater
  20. */
  21. function __construct($time=0, $format=''){
  22. $this->time = $time;
  23. $this->format = $format;
  24. }
  25. /**
  26. * Singleton Method
  27. */
  28. public function get(){
  29. static $instance;
  30. if (!isset($instance)) {
  31. $instance = new RMTimeFormatter();
  32. }
  33. return $instance;
  34. }
  35. public function format($time=0, $format=''){
  36. $time = $time<=0 ? $this->time : $time;
  37. $format = $format=='' ? $this->format : $format;
  38. if ($format=='' || $time<0){
  39. trigger_error(__('You must provide a valid time and format value to use RMTimeFormatter::format() method','rmcommon'));
  40. return;
  41. }
  42. $find = array(
  43. '%d%', // Day number
  44. '%D%', // Day name
  45. '%m%', // Month number
  46. '%M%', // Month name
  47. '%T%', // Month three letters
  48. '%y%', // Year with two digits (e.g. 04, 05, etc.)
  49. '%Y%', // Year with four digits (e.g. 2004, 2005, etc.)
  50. '%h%', // Hour
  51. '%i%', // Minute
  52. '%s%' // Second
  53. );
  54. $replace = array(
  55. date('d', $time),
  56. $this->days($time),
  57. date('m', $time),
  58. $this->months($time),
  59. substr($this->months($time), 0, 3),
  60. date('y', $time),
  61. date('Y', $time),
  62. date('H', $time),
  63. date('i', $time),
  64. date('s', $time)
  65. );
  66. return str_replace($find, $replace, $format);
  67. }
  68. /**
  69. * Day name for time formatting
  70. *
  71. * @param int $time
  72. * @return string Day name
  73. */
  74. public function days($time = 0){
  75. $time = $time<=0 ? $this->time : $time;
  76. if($time<=0) return;
  77. $days = array(
  78. __('Sunday','rmcommon'),
  79. __('Monday','rmcommon'),
  80. __('Tuesday','rmcommon'),
  81. __('Wednesday','rmcommon'),
  82. __('Thursday','rmcommon'),
  83. __('Friday','rmcommon'),
  84. __('Saturday','rmcommon')
  85. );
  86. return $days[date("w", $time)];
  87. }
  88. public function months($time=0){
  89. $time = $time<=0 ? $this->time : $time;
  90. if($time<=0) return;
  91. $months = array(
  92. __('January', 'rmcommon'),
  93. __('February', 'rmcommon'),
  94. __('March', 'rmcommon'),
  95. __('April', 'rmcommon'),
  96. __('May', 'rmcommon'),
  97. __('June', 'rmcommon'),
  98. __('July', 'rmcommon'),
  99. __('August', 'rmcommon'),
  100. __('September', 'rmcommon'),
  101. __('October', 'rmcommon'),
  102. __('November', 'rmcommon'),
  103. __('December', 'rmcommon'),
  104. );
  105. return $months[date('n', $time)-1];
  106. }
  107. }