DateHelper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * DateHelper.
  11. *
  12. * @package symfony
  13. * @subpackage helper
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: DateHelper.php 11783 2008-09-25 16:21:27Z fabien $
  16. */
  17. function format_daterange($start_date, $end_date, $format = 'd', $full_text, $start_text, $end_text, $culture = null, $charset = null)
  18. {
  19. if ($start_date != '' && $end_date != '')
  20. {
  21. return sprintf($full_text, format_date($start_date, $format, $culture, $charset), format_date($end_date, $format, $culture, $charset));
  22. }
  23. else if ($start_date != '')
  24. {
  25. return sprintf($start_text, format_date($start_date, $format, $culture, $charset));
  26. }
  27. else if ($end_date != '')
  28. {
  29. return sprintf($end_text, format_date($end_date, $format, $culture, $charset));
  30. }
  31. }
  32. function format_date($date, $format = 'd', $culture = null, $charset = null)
  33. {
  34. static $dateFormats = array();
  35. if (is_null($date))
  36. {
  37. return null;
  38. }
  39. if (!$culture)
  40. {
  41. $culture = sfContext::getInstance()->getUser()->getCulture();
  42. }
  43. if (!$charset)
  44. {
  45. $charset = sfConfig::get('sf_charset');
  46. }
  47. if (!isset($dateFormats[$culture]))
  48. {
  49. $dateFormats[$culture] = new sfDateFormat($culture);
  50. }
  51. return $dateFormats[$culture]->format($date, $format, null, $charset);
  52. }
  53. function format_datetime($date, $format = 'F', $culture = null, $charset = null)
  54. {
  55. return format_date($date, $format, $culture, $charset);
  56. }
  57. function distance_of_time_in_words($from_time, $to_time = null, $include_seconds = false)
  58. {
  59. $to_time = $to_time? $to_time: time();
  60. $distance_in_minutes = floor(abs($to_time - $from_time) / 60);
  61. $distance_in_seconds = floor(abs($to_time - $from_time));
  62. $string = '';
  63. $parameters = array();
  64. if ($distance_in_minutes <= 1)
  65. {
  66. if (!$include_seconds)
  67. {
  68. $string = $distance_in_minutes == 0 ? 'less than a minute' : '1 minute';
  69. }
  70. else
  71. {
  72. if ($distance_in_seconds <= 5)
  73. {
  74. $string = 'less than 5 seconds';
  75. }
  76. else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10)
  77. {
  78. $string = 'less than 10 seconds';
  79. }
  80. else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20)
  81. {
  82. $string = 'less than 20 seconds';
  83. }
  84. else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40)
  85. {
  86. $string = 'half a minute';
  87. }
  88. else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59)
  89. {
  90. $string = 'less than a minute';
  91. }
  92. else
  93. {
  94. $string = '1 minute';
  95. }
  96. }
  97. }
  98. else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44)
  99. {
  100. $string = '%minutes% minutes';
  101. $parameters['%minutes%'] = $distance_in_minutes;
  102. }
  103. else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89)
  104. {
  105. $string = 'about 1 hour';
  106. }
  107. else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439)
  108. {
  109. $string = 'about %hours% hours';
  110. $parameters['%hours%'] = round($distance_in_minutes / 60);
  111. }
  112. else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879)
  113. {
  114. $string = '1 day';
  115. }
  116. else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199)
  117. {
  118. $string = '%days% days';
  119. $parameters['%days%'] = round($distance_in_minutes / 1440);
  120. }
  121. else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399)
  122. {
  123. $string = 'about 1 month';
  124. }
  125. else if ($distance_in_minutes >= 86400 && $distance_in_minutes <= 525959)
  126. {
  127. $string = '%months% months';
  128. $parameters['%months%'] = round($distance_in_minutes / 43200);
  129. }
  130. else if ($distance_in_minutes >= 525960 && $distance_in_minutes <= 1051919)
  131. {
  132. $string = 'about 1 year';
  133. }
  134. else
  135. {
  136. $string = 'over %years% years';
  137. $parameters['%years%'] = floor($distance_in_minutes / 525960);
  138. }
  139. if (sfConfig::get('sf_i18n'))
  140. {
  141. require_once dirname(__FILE__).'/I18NHelper.php';
  142. return __($string, $parameters);
  143. }
  144. else
  145. {
  146. return strtr($string, $parameters);
  147. }
  148. }
  149. // Like distance_of_time_in_words, but where to_time is fixed to time()
  150. function time_ago_in_words($from_time, $include_seconds = false)
  151. {
  152. return distance_of_time_in_words($from_time, time(), $include_seconds);
  153. }