You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Date.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * This file is part of Twig.
  4. *
  5. * (c) 2014 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Symfony\Component\Translation\TranslatorInterface;
  11. /**
  12. * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
  13. */
  14. class Twig_Extensions_Extension_Date extends Twig_Extension
  15. {
  16. public static $units = array(
  17. 'y' => 'year',
  18. 'm' => 'month',
  19. 'd' => 'day',
  20. 'h' => 'hour',
  21. 'i' => 'minute',
  22. 's' => 'second',
  23. );
  24. /**
  25. * @var TranslatorInterface
  26. */
  27. private $translator;
  28. /**
  29. * Constructor.
  30. *
  31. * @param TranslatorInterface $translator A TranslatorInterface instance.
  32. */
  33. public function __construct(TranslatorInterface $translator = null)
  34. {
  35. $this->translator = $translator;
  36. }
  37. /**
  38. * Returns a list of filters.
  39. *
  40. * @return array
  41. */
  42. public function getFilters()
  43. {
  44. return array(
  45. new Twig_SimpleFilter('time_diff', array($this, 'diff'), array(
  46. 'needs_environment' => true,
  47. )),
  48. );
  49. }
  50. /**
  51. * Name of this extension.
  52. *
  53. * @return string
  54. */
  55. public function getName()
  56. {
  57. return 'date';
  58. }
  59. /**
  60. * Filter for converting dates to a time ago string like Facebook and Twitter has.
  61. *
  62. * @param Twig_Environment $env A Twig_Environment instance.
  63. * @param string|DateTime $date A string or DateTime object to convert.
  64. * @param string|DateTime $now A string or DateTime object to compare with. If none given, the current time will be used.
  65. *
  66. * @return string The converted time.
  67. */
  68. public function diff(Twig_Environment $env, $date, $now = null)
  69. {
  70. // Convert both dates to DateTime instances.
  71. $date = twig_date_converter($env, $date);
  72. $now = twig_date_converter($env, $now);
  73. // Get the difference between the two DateTime objects.
  74. $diff = $date->diff($now);
  75. // Check for each interval if it appears in the $diff object.
  76. foreach (self::$units as $attribute => $unit) {
  77. $count = $diff->$attribute;
  78. if (0 !== $count) {
  79. return $this->getPluralizedInterval($count, $diff->invert, $unit);
  80. }
  81. }
  82. return '';
  83. }
  84. protected function getPluralizedInterval($count, $invert, $unit)
  85. {
  86. if ($this->translator) {
  87. $id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit);
  88. return $this->translator->transChoice($id, $count, array('%count%' => $count), 'date');
  89. }
  90. if ($count > 1) {
  91. $unit .= 's';
  92. }
  93. return $invert ? "in $count $unit" : "$count $unit ago";
  94. }
  95. }