Skip to content

Commit

Permalink
doc: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jul 31, 2023
1 parent 22e7ca8 commit 246d2fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
74 changes: 48 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Friendly *ago* dates ("5 minutes ago")!
# knplabs/knp-time-bundle

This bundle does one simple job: takes dates and gives you friendly "2 hours ago"-type messages. Woh!
Friendly *ago*/*until* dates ("5 minutes ago" or "in 5 minutes") and *durations* ("2 mins")!

```html+jinja
Last edited {{ post.updatedAt|ago }}
<-- Last edited 1 week ago -->
```twig
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 mins -->
```

Want to see it used in a screencast 🎥? Check out SymfonyCasts: https://symfonycasts.com/screencast/symfony-doctrine/ago

The date formatted can be translated into any language, and many are supported out of the box.
The formatted date/duration can be translated into any language, and many are supported out of the box.

## INSTALLATION
## Installation

Use Composer to install the library:

Expand All @@ -23,56 +26,75 @@ Woo! You did it! Assuming your project uses Symfony Flex, the
bundle should be configured and ready to go. If not, you
can enable `Knp\Bundle\TimeBundle\KnpTimeBundle` manually.

## USAGE
## Usage

### Twig

In Twig:
Time formatting:

```twig
{{ someDateTimeVariable|ago }}
{{ someDateTimeVariable|time_diff }} {# 2 weeks ago #}
... or use the equivalent function:
{{ time_diff(someDateTimeVariable) }}
{# |ago is an alias for |time_diff #}
{{ someDateTimeVariable|ago }} {# 1 second ago #}
{# ... or use the equivalent function: #}
{{ time_diff(someDateTimeVariable) }} {# in 2 months #}
```

Note: the "ago" filter works fine for dates in the future, too.
> **Note**: the `time_diff` filter/function and `ago` alias works fine for dates in the future, too.
### In controllers
Duration formatting:

You can also "ago" dates inside PHP by autowiring the `Knp\Bundle\TimeBundle\DateTimeFormatter` service:
```twig
{{ someDurationInSeconds|duration }} {# 2 mins #}
```

### Service

You can also format dates and durations in your services/controllers by autowiring/injecting the
`Knp\Bundle\TimeBundle\DateTimeFormatter` service:

```
use Knp\Bundle\TimeBundle\DateTimeFormatter;
// ...
public function yourAction(DateTimeFormatter $dateTimeFormatter)
{
$someDate = new \DateTime('2017-02-11'); //or $entity->publishedDate()
$now = new \DateTime();
$agoTime = $dateTimeFormatter->formatDiff($someDate, $now);
$someDate = new \DateTimeImmutable('2017-02-11'); // or $entity->publishedDate()
$toDate = new \DateTimeImmutable('now');
$agoTime = $dateTimeFormatter->formatDiff($someDate, $toDate); // $toDate parameter is optional and defaults to "now"
$readTime = $dateTimeFormatter->formatDuration(64); // or $entity->readTimeInSeconds()
return $this->json([
...
'published_at' => $agoTime
...
// ...
'published_at' => $agoTime, // 2 years ago
'read_time' => $readTime, // 1 min
// ...
]);
}
```

## Controlling the Translation Locale

The bundle will automatically use the current locale when translating
the "ago" messages. However, you can override the locale:
the "time_diff" ("ago") and "duration" messages. However, you can override
the locale:

```twig
{{ someDateTimeVariable|ago(locale='es') }}
{{ someDateTimeVariable|time_diff(locale='es') }}
{{ someDurationInSeconds|duration(locale='es') }}
```

## TESTS
## Tests

If you want to run tests, please check that you have installed dev dependencies.

```bash
./vendor/bin/simple-phpunit
./vendor/bin/phpunit
```

## Maintainers
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "knplabs/knp-time-bundle",
"type": "symfony-bundle",
"description": "Making your dates look sensible and descriptive",
"keywords": ["time", "date", "descriptive time", "knplabs", "knp", "bundle"],
"description": "Making your dates and durations look sensible and descriptive",
"keywords": ["time", "date", "descriptive time", "knplabs", "knp", "bundle", "duration"],
"homepage": "https://github.com/KnpLabs/KnpTimeBundle",
"license": "MIT",

Expand Down Expand Up @@ -33,7 +33,7 @@
},

"suggest": {
"symfony/twig-bundle": "to use the Twig `time_diff()` function or `|time_diff` filter"
"symfony/twig-bundle": "to use the Twig `time_diff()` function and `|time_diff`/`|duration` filter"
},

"conflict": {
Expand Down

0 comments on commit 246d2fe

Please sign in to comment.