Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add age formatter #192

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Last edited: {{ post.updatedAt|time_diff }} <!-- Last edited: 1 week ago -->
Event date: {{ event.date|time_diff }} <!-- Event date: in two weeks -->

Read time: {{ post.readTimeInSeconds|duration }} <!-- Read time: 2 minutes -->

Age: {{ user.birthdate|age }} <!-- Age: 30 years old -->
```

Want to see it used in a screencast 🎥? Check out SymfonyCasts: https://symfonycasts.com/screencast/symfony-doctrine/ago
Expand Down Expand Up @@ -50,6 +52,16 @@ Duration formatting:
{{ someDurationInSeconds|duration }} {# 2 minutes #}
```

Age formatting:

```twig
{# with filter: #}
Age: {{ user.birthdate|age }} {# Age: 30 years old #}

{# ... or use the equivalent function: #}
Age: {{ age(user.birthdate) }} {# Age: 30 years old #}
```

### Service

You can also format dates and durations in your services/controllers by autowiring/injecting the
Expand All @@ -68,6 +80,8 @@ public function yourAction(DateTimeFormatter $dateTimeFormatter)

$readTime = $dateTimeFormatter->formatDuration(64); // or $entity->readTimeInSeconds()

$ageTime = $dateTimeFormatter->formatAge($someDate, $toDate); // $toDate parameter is optional and defaults to "now"

return $this->json([
// ...
'published_at' => $agoTime, // 2 years ago
Expand All @@ -87,6 +101,8 @@ the locale:
{{ someDateTimeVariable|time_diff(locale='es') }}

{{ someDurationInSeconds|duration(locale='es') }}

{{ someDateTimeVariable|age(locale='es') }}
```

## Tests
Expand Down
16 changes: 16 additions & 0 deletions src/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ public function formatDuration(float $seconds, string $locale = null): string
return $this->translator->trans('duration.none', [], 'time', $locale);
}

/**
* Returns a formatted age for the given from and to datetimes.
*/
public function formatAge(
int|string|\DateTimeInterface $from,
int|string|\DateTimeInterface $to = null,
string $locale = null
): string {
$from = self::formatDateTime($from);
$to = self::formatDateTime($to);

$diff = $from->diff($to);

return $this->translator->trans('age', ['%count%' => $diff->y], 'time', $locale);
}

private static function formatDateTime(int|string|\DateTimeInterface|null $value): \DateTimeInterface
{
if ($value instanceof \DateTimeInterface) {
Expand Down
10 changes: 10 additions & 0 deletions src/Twig/Extension/TimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function getFunctions(): array
[DateTimeFormatter::class, 'formatDiff'],
['is_safe' => ['html']]
),
new TwigFunction(
'age',
[DateTimeFormatter::class, 'formatAge'],
['is_safe' => ['html']]
),
];
}

Expand All @@ -46,6 +51,11 @@ public function getFilters(): array
[DateTimeFormatter::class, 'formatDuration'],
['is_safe' => ['html']]
),
new TwigFilter(
'age',
[DateTimeFormatter::class, 'formatAge'],
['is_safe' => ['html']]
),
];
}
}
4 changes: 4 additions & 0 deletions translations/time.en.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<source>duration.none</source>
<target>&lt; 1 second</target>
</trans-unit>
<trans-unit id="19">
<source>age</source>
<target>1 year old|%count% years old</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions translations/time.fr.xliff
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<source>duration.none</source>
<target>&lt; 1 seconde</target>
</trans-unit>
<trans-unit id="19">
<source>age</source>
<target>1 an|%count% ans</target>
</trans-unit>
</body>
</file>
</xliff>