Giter VIP home page Giter VIP logo

relative-time's Introduction

salernolabs/relative-time

Latest Stable Version License Build Status

Library for building relative time strings in PHP.

Usage

Installation

First include this project with composer

composer require salernolabs/relative-time

Formatting Time

Then run the formatter on a DateTime object.

$formatter = new \SalernoLabs\RelativeTime\Formatter();
$relativeTime = $formatter->getRelativeTime(new \DateTime('-47 minutes'));
echo $relativeTime;

This would output '47 minutes ago'. The formatter will also go forward as well so if you put in '+47 minutes' it will say "47 mintues from now".

Examples of Time Reformatting

This library will also give short-hand conversational outputs for some values. For example:

Time Modification Output
-5 minutes just now
+5 minutes momentarily
-1 minute about a minute ago
+1 minute about a minute from now
-1 hour about an hour ago
+1 hour about an hour from now
-1 day yesterday
+1 day tomorrow

And actual numbers for other times, for example:

Time Modification Output
-45 seconds 45 seconds ago
-45 minutes 45 minutes ago
-7 hours 7 hours ago
-3 years 3 years ago

HTML5 Tags

You can easily wrap the output in an html5 time tag by using the getRelativeTimeTag function.

$formatter = new \SalernoLabs\RelativeTime\Formatter();
$relativeTime = $formatter->getRelativeTimeTag(new \DateTime('-47 minutes'));
echo $relativeTime;

This would output:

<time title="April 14th, 2017 - 4:14PM EDT">47 minutes ago</time>

Relative Time Clock

The relative time clock can give you a textual representation of a time of day.

$clock = new \SalernoLabs\RelativeTime\Clock();

$time = $clock
    ->setTime(new \DateTime('7:47'))
    ->getTime();

echo $time; // a quarter to eight o'clock

Examples of Relative Clock Output

Input Time Output
1:00 one o'clock
5:03 five o'clock,
2:07 almost ten after two o'clock,
3:10 ten after three o'clock
4:15 quarter after four o'clock
6:22 almost half past six o'clock
7:32 half past seven o'clock
8:44 almost a quarter to nine o'clock
9:45 a quarter to ten o'clock
11:55 almost twelve o'clock
12:00 twelve o'clock,
12:59 almost one o'clock
12:45 a quarter to one o'clock
12:43 almost a quarter to one o'clock

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.