Skip to content

[Translation] Add human readable DateInterval #29929

@Deltachaos

Description

@Deltachaos

Implementing a human readable duration and since time representation is hard if you want to consider I10N as well. The https://symfony.com/doc/current/components/console/helpers/progressbar.html component already uses a human readable DateInterval representation. We should be able to use this feature not only in a console context.

Example

<?php
$formater = new DateIntervalFormater($translator, [
  'spellInFullUntil' => 7
]);
$formater->humanizeInterval(new \DateInterval('P1MT5H50S')) ===
    'one month, five houres and 50 seconds';
$formater->humanizeDate(new \DateTime('2040-01-20')) ===
    'in 40 years and two days';
$formater->humanizeDate(new \DateTime('2040-01-20'), new \DateTime('2018-01-18')) ===
    'in 40 years and two days';
$formater->humanizeDate(new \DateTime('2018-01-01')) ===
    'one year and 18 days ago';
<!-- one month, five houres and 50 seconds -->
<span>Elapsed time: {{ 'P1MT5H50S' | date_interval }}</span>
<!-- one minute -->
<span>Elapsed time: {{ 60 | date_interval }}</span>
<!-- pass DateInterval object -->
<span>Elapsed time: {{ someInterval | date_interval }}</span>
<!-- in 40 years and two days -->
<span>Published {{ '2040-01-20' | date_relative }}</span>
<!-- one year and 18 days ago -->
<span>Published {{ '2018-01-01' | date_relative('2040-01-18') }}</span>
<!-- pass some DateTime object -->
<span>Published {{ dateObject | date_relative('2040-01-18') }}</span>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions