2017 © Pedro Peláez
 

library php-pretty-datetime

Generates human-readable strings for PHP DateTime objects

image

danielstjules/php-pretty-datetime

Generates human-readable strings for PHP DateTime objects

  • Tuesday, December 16, 2014
  • by danielstjules
  • Repository
  • 3 Watchers
  • 45 Stars
  • 12,797 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 1 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

php-pretty-datetime

Generates human-readable strings for PHP DateTime objects. It handles dates in the past and future. For future dates, it uses the format 'In x unit', ie: 'In 1 minute'. For dates in the past, it uses 'x unit ago', ie: '2 years ago'., (*1)

Note: Comparison of dates, for those beyond a day apart, uses the difference between their Unix timestamps., (*2)

Installation

If you're using Composer to manage dependencies, you can include the following in your composer.json file:, (*3)

"require": {
    "danielstjules/php-pretty-datetime": "~1.0.0"
}

Otherwise, you can simply require the file directly:, (*4)

require_once 'path/to/php-pretty-datetime/src/PrettyDateTime.php';

Usage

use PrettyDateTime\PrettyDateTime;

PrettyDateTime::parse(new DateTime('now'));         // Moments ago
PrettyDateTime::parse(new DateTime('+ 59 second')); // Seconds from now
PrettyDateTime::parse(new DateTime('+ 1 minute'));  // In 1 minute
PrettyDateTime::parse(new DateTime('- 59 minute')); // 59 minutes ago

// You can supply a secondary argument to provide an alternate reference
// DateTime. The default is the current DateTime, ie: DateTime('now'). In
// addition, it takes into account the day of each DateTime. So in the next
// two examples, even though they're only a second apart, 'Yesterday' and
// 'Tomorrow' will be displayed

$now = new DateTime('1991-05-18 00:00:00 UTC');
$dateTime = new DateTime('1991-05-17 23:59:59 UTC');
PrettyDateTime::parse($dateTime, $now); // Yesterday

$now = new DateTime('1991-05-17 23:59:59 UTC');
$dateTime = new DateTime('1991-05-18 00:00:00 UTC');
PrettyDateTime::parse($dateTime, $now) // Tomorrow

Tests

Build Status, (*5)

From the project directory, tests can be ran using phpunit, (*6)

License

Released under the MIT License - see LICENSE.txt for details., (*7)

The Versions

16/12 2014

dev-master

9999999-dev

Generates human-readable strings for PHP DateTime objects

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

date time string datetime human pretty readable