nehlsen/time-formatter-bundle

Small Utility to format remaining time of something into a human-readable form

2.0.1 2024-09-28 08:22 UTC

This package is auto-updated.

Last update: 2024-09-28 09:16:20 UTC


README

Small Utility class to format remaining time of something into a human-readable form.

Example

Given Milk having a bestBefore Timestamp, we can calculate its remaining lifetime like so

/** @var Milk $myMilk */
$myMilk = $fridge->getMyMilk();
/** @var \DateTimeImmutable $bestBefore */
$bestBefore = $myMilk->getBestoBefore(); 

$secondsUntilBad = $bestBefore->getTimestamp() - time();

$formatter = new \nehlsen\TimeFormatterBundle\TimeFormatter\TimeFormatter();
$formattedBestBefore = $formatter->format($secondsUntilBad);
// $formattedBestBefore == '3 Days 1 Hour 17 Seconds';

For a range of options and examples see example1.php

Installation

  1. Require the library
    composer require nehlsen/time-formatter
  2. For the translations to work, the provided files can be used, copied, linked. But they are not enabled automatically.