Date.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. use Symfony\Component\Translation\IdentityTranslator;
  12. /**
  13. * @author Robin van der Vleuten <robinvdvleuten@gmail.com>
  14. */
  15. class Twig_Extensions_Extension_Date extends Twig_Extension
  16. {
  17. public static $units = array(
  18. 'y' => 'year',
  19. 'm' => 'month',
  20. 'd' => 'day',
  21. 'h' => 'hour',
  22. 'i' => 'minute',
  23. 's' => 'second',
  24. );
  25. /**
  26. * @var TranslatorInterface
  27. */
  28. private $translator;
  29. public function __construct(TranslatorInterface $translator = null)
  30. {
  31. // Ignore the IdentityTranslator, otherwise the parameters won't be replaced properly
  32. if ($translator instanceof IdentityTranslator) {
  33. $translator = null;
  34. }
  35. $this->translator = $translator;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getFilters()
  41. {
  42. return array(
  43. new Twig_SimpleFilter('time_diff', array($this, 'diff'), array('needs_environment' => true)),
  44. );
  45. }
  46. /**
  47. * Filter for converting dates to a time ago string like Facebook and Twitter has.
  48. *
  49. * @param Twig_Environment $env a Twig_Environment instance
  50. * @param string|DateTime $date a string or DateTime object to convert
  51. * @param string|DateTime $now A string or DateTime object to compare with. If none given, the current time will be used.
  52. *
  53. * @return string the converted time
  54. */
  55. public function diff(Twig_Environment $env, $date, $now = null)
  56. {
  57. // Convert both dates to DateTime instances.
  58. $date = twig_date_converter($env, $date);
  59. $now = twig_date_converter($env, $now);
  60. // Get the difference between the two DateTime objects.
  61. $diff = $date->diff($now);
  62. // Check for each interval if it appears in the $diff object.
  63. foreach (self::$units as $attribute => $unit) {
  64. $count = $diff->$attribute;
  65. if (0 !== $count) {
  66. return $this->getPluralizedInterval($count, $diff->invert, $unit);
  67. }
  68. }
  69. return '';
  70. }
  71. protected function getPluralizedInterval($count, $invert, $unit)
  72. {
  73. if ($this->translator) {
  74. $id = sprintf('diff.%s.%s', $invert ? 'in' : 'ago', $unit);
  75. return $this->translator->transChoice($id, $count, array('%count%' => $count), 'date');
  76. }
  77. if (1 !== $count) {
  78. $unit .= 's';
  79. }
  80. return $invert ? "in $count $unit" : "$count $unit ago";
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getName()
  86. {
  87. return 'date';
  88. }
  89. }
  90. class_alias('Twig_Extensions_Extension_Date', 'Twig\Extensions\DateExtension', false);