2017 © Pedro Peláez
 

library php-datetime-ago

How much time ago in text format

image

smirik/php-datetime-ago

How much time ago in text format

  • Tuesday, September 8, 2015
  • by smirik
  • Repository
  • 3 Watchers
  • 18 Stars
  • 8,082 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 6 Versions
  • 13 % Grown

The README.md

php-datetime-ago

This library helps to format the date/time interval into text., (*1)

Examples

Condition Text
Now now
Less than an hour xx minutes ago
Between 1 and 2 hours 1 hour ago
Between 2 and 24 hours xx hours ago
Between 1 and 2 days 1 day ago
Between 2 and 6 days xx days ago
7 or more days ago xx weeks ago
30 or more days ago xx months ago
365 or more days ago xx years ago

Notice that not every translator supports weeks, months and years, (*2)

Usage

DateTimeAgo class has public method get. It accepts 1 required argument (\DateTime object) and 1 optional (reference date. If it is null, current time is used)., (*3)

<?php
    $date_ago = new \Smirik\PHPDateTimeAgo\DateTimeAgo();
    $date_ago->get(new \DateTime('-24 hours')); /* returns "1 day ago" */
    $date_ago->get(new \DateTime('-69 minutes')); /* returns "1 hour ago" */
    $date_ago->get(new \DateTime('-100 minutes')); /* returns "2 hours ago" */
    $date_ago->get(new \DateTime('-155 minutes')); /* returns "3 hours ago" */

Russian translation:, (*4)

<?php
    $date_ago = new \Smirik\PHPDateTimeAgo\DateTimeAgo(new \Smirik\PHPDateTimeAgo\TextTranslator\RussianTextTranslator());
    $date_ago->get(new \DateTime('-24 hours')); /* returns "1 день назад" */
    $date_ago->get(new \DateTime('-69 minutes')); /* returns "1 час назад" */
    $date_ago->get(new \DateTime('-100 minutes')); /* returns "2 часа назад" */
    $date_ago->get(new \DateTime('-155 minutes')); /* returns "3 часа назад" */

Customization

Date formatter for more than 6 days

<?php
    ...
    $date_ago->setFormat('d.m.Y H:i:s');

This setup date in format 01.01.2001 22:52:12., (*5)

Custom translator

The DateTimeAgo constructor accept TextTranslator file as the first argument. By default EnglishTextTranslator is provided. You can add your custom translator to this constructor. Just make sure that your class implements Smirik\PHPDateTimeAgo\TextTranslator\TextTranslatorInterface. There is also standard way for translations based on pluralization procedure. The methods are already implemented for any language in AbstractTextTranslator class. English & Russian samples are already included in this package. You can also change the translator after creation:, (*6)

<?php
    ...
    $date_ago->setTextTranslator(new \Smirik\PHPDateTimeAgo\TextTranslator\RussianTextTranslator());

Implementation

Tests

Most parts of the code are tested via phpspec. To run the tests clone the repository and run, (*7)

$ php bin/phpspec run

Development workflow

  • In order to keep git versioning accurate please use git-flow pattern.
  • So all pull requests except hotfix should go to the develop branch.
  • Hotfix should go to both development branches: develop & master at the same time.

Contribution

Any contribution is welcome., (*8)

Acknowledgements

The Versions

08/09 2015

dev-master

9999999-dev

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

08/09 2015

v1.1.0

1.1.0.0

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

08/09 2015

dev-develop

dev-develop

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

29/07 2013

v1.0.1

1.0.1.0

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

29/07 2013

v1.0

1.0.0.0

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

22/07 2013

v0.1

0.1.0.0

How much time ago in text format

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires