MyTwigExtension.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Muzich\CoreBundle\Extension;
  3. use Symfony\Bundle\FrameworkBundle\Translation\Translator;
  4. class MyTwigExtension extends \Twig_Extension {
  5. private $translator;
  6. public function __construct(Translator $translator)
  7. {
  8. $this->translator = $translator;
  9. }
  10. public function getFilters()
  11. {
  12. return array(
  13. 'var_dump' => new \Twig_Filter_Function('var_dump'),
  14. 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date')
  15. );
  16. }
  17. protected function datetime2timestamp($string)
  18. {
  19. list($date, $time) = explode(' ', $string);
  20. list($year, $month, $day) = explode('-', $date);
  21. list($hour, $minute, $second) = explode(':', $time);
  22. $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
  23. return $timestamp;
  24. }
  25. public function date_or_relative_date($sentence, $expr = null)
  26. {
  27. $iTimeDifference = time() - $this->datetime2timestamp($sentence);
  28. if( $iTimeDifference<0 )
  29. {
  30. return $this->translator->trans('date.instant', array(), 'userui');;
  31. }
  32. $iSeconds = $iTimeDifference ;
  33. $iMinutes = round( $iTimeDifference/60 );
  34. $iHours = round( $iTimeDifference/3600 );
  35. $iDays = round( $iTimeDifference/86400 );
  36. $iWeeks = round( $iTimeDifference/604800 );
  37. $iMonths = round( $iTimeDifference/2419200 );
  38. $iYears = round( $iTimeDifference/29030400 );
  39. if( $iSeconds<60 )
  40. {
  41. return $this->translator->trans('date.less_than_minute', array(), 'userui');
  42. }
  43. elseif( $iMinutes<60 )
  44. {
  45. return $this->translator->transChoice(
  46. 'il y a une minute|Il y a %count% minutes',
  47. $iMinutes,
  48. array('%count%' => $iMinutes)
  49. );
  50. }
  51. elseif( $iHours<24 )
  52. {
  53. return $this->translator->transChoice(
  54. 'il y a une heure|Il y a %count% heures',
  55. $iHours,
  56. array('%count%' => $iHours)
  57. );
  58. }
  59. elseif( $iDays<7 )
  60. {
  61. return $this->translator->transChoice(
  62. 'il y a un jour|Il y a %count% jours',
  63. $iDays,
  64. array('%count%' => $iDays)
  65. );
  66. }
  67. elseif( $iWeeks <4 )
  68. {
  69. return $this->translator->transChoice(
  70. 'il y a une semaine|Il y a %count% semaines',
  71. $iWeeks,
  72. array('%count%' => $iWeeks)
  73. );
  74. }
  75. elseif( $iMonths<12 )
  76. {
  77. return $this->translator->transChoice(
  78. 'il y a un mois|Il y a %count% mois',
  79. $iMonths,
  80. array('%count%' => $iMonths)
  81. );
  82. }
  83. else
  84. {
  85. return $this->translator->transChoice(
  86. 'il y a un an|Il y a %count% ans',
  87. $iYears,
  88. array('%count%' => $iYears)
  89. );
  90. }
  91. }
  92. public function getName()
  93. {
  94. return 'my_twig_extension';
  95. }
  96. }