vendor/knplabs/knp-time-bundle/src/DateTimeFormatter.php line 95

Open in your IDE?
  1. <?php
  2. namespace Knp\Bundle\TimeBundle;
  3. use Symfony\Contracts\Translation\TranslatorInterface;
  4. final class DateTimeFormatter
  5. {
  6. /**
  7. * @internal
  8. */
  9. public function __construct(private TranslatorInterface $translator)
  10. {
  11. }
  12. /**
  13. * Returns a formatted diff for the given from and to datetimes.
  14. */
  15. public function formatDiff(
  16. int|string|\DateTimeInterface $from,
  17. int|string|\DateTimeInterface $to = null,
  18. string $locale = null
  19. ): string {
  20. $from = self::formatDateTime($from);
  21. $to = self::formatDateTime($to);
  22. static $units = [
  23. 'y' => 'year',
  24. 'm' => 'month',
  25. 'd' => 'day',
  26. 'h' => 'hour',
  27. 'i' => 'minute',
  28. 's' => 'second',
  29. ];
  30. $diff = $to->diff($from);
  31. foreach ($units as $attribute => $unit) {
  32. $count = $diff->$attribute;
  33. if (0 !== $count) {
  34. $id = sprintf('diff.%s.%s', $diff->invert ? 'ago' : 'in', $unit);
  35. return $this->translator->trans($id, ['%count%' => $count], 'time', $locale);
  36. }
  37. }
  38. return $this->translator->trans('diff.empty', [], 'time', $locale);
  39. }
  40. /**
  41. * @author Fabien Potencier <fabien@symfony.com>
  42. *
  43. * @source https://github.com/symfony/symfony/blob/ad72245261792c6b5d2db821fcbd141b11095215/src/Symfony/Component/Console/Helper/Helper.php#L97
  44. */
  45. public function formatDuration(float $seconds, string $locale = null): string
  46. {
  47. static $timeFormats = [
  48. [0, 'duration.none'],
  49. [1, 'duration.second'],
  50. [2, 'duration.second', 1],
  51. [60, 'duration.minute'],
  52. [120, 'duration.minute', 60],
  53. [3600, 'duration.hour'],
  54. [7200, 'duration.hour', 3600],
  55. [86400, 'duration.day'],
  56. [172800, 'duration.day', 86400],
  57. ];
  58. foreach ($timeFormats as $index => $format) {
  59. if ($seconds >= $format[0]) {
  60. if ((isset($timeFormats[$index + 1]) && $seconds < $timeFormats[$index + 1][0])
  61. || $index === \count($timeFormats) - 1
  62. ) {
  63. if (2 === \count($format)) {
  64. return $this->translator->trans($format[1], ['%count%' => 1], 'time', $locale);
  65. }
  66. return $this->translator->trans(
  67. $format[1],
  68. ['%count%' => floor($seconds / $format[2])],
  69. 'time',
  70. $locale
  71. );
  72. }
  73. }
  74. }
  75. return $this->translator->trans('duration.none', [], 'time', $locale);
  76. }
  77. /**
  78. * Returns a formatted age for the given from and to datetimes.
  79. */
  80. public function formatAge(
  81. int|string|\DateTimeInterface $from,
  82. int|string|\DateTimeInterface $to = null,
  83. string $locale = null
  84. ): string {
  85. $from = self::formatDateTime($from);
  86. $to = self::formatDateTime($to);
  87. $diff = $from->diff($to);
  88. return $this->translator->trans('age', ['%count%' => $diff->y], 'time', $locale);
  89. }
  90. private static function formatDateTime(int|string|\DateTimeInterface|null $value): \DateTimeInterface
  91. {
  92. if ($value instanceof \DateTimeInterface) {
  93. return $value;
  94. }
  95. if (is_int($value)) {
  96. $value = date('Y-m-d H:i:s', $value);
  97. }
  98. if (null === $value) {
  99. $value = 'now';
  100. }
  101. return new \DateTime($value);
  102. }
  103. }