Giter Club home page Giter Club logo

stamp's Introduction

PHP Stamp

Simple date formatting for humans. Based on the ruby package of the same name, stamp allows you to easily format dates without having to remember arcane arguments to pass to date().

Status

Stamp should be fully functional, it passes all the tests that it's ruby cousin does. The code needs a bit of cleanup but it is perfectly usable as is.

Performance

Stamp works by parsing the example string and building up an array of formatting objects for each part of the example. This obviously involves creating multiple objects and is thus a heavier solution than just using php's date() function. But, as with most development tools, the tradeoff for slightly decreased performance is easier development.

To help with performance, the parser is cached per example string. You should only create one Stamp object per view/page and reuse it for each date format. Stamp will cache each example string parser so that if you loop through a set of dates and format each using the same example, the example will only be parsed once.

On my MacBook Pro stamp can format 10,000 different dates (using different example strings) in about 0.75 seconds and can format 10,000 different dates (using the same example string) in about 0.2 seconds.

TODO

  • Adhere to PSR-2 standards
  • Internationalisation?

Usage

Stamp can be loaded using the Composer autoloader or an autoloader of your choice.

Usage is simple:

$stamp = new Stamp\Stamp();
$stamp->stamp("August 14th 2012", time()); // February 9th 2013
$stamp->stamp("04/23/2012 @ 4:13PM (Saturday)", time()) // 02/09/2013 @ 10:50AM (Sunday)

For a more complete list of examples, check out the Test Suite

Limitations

There are some instances where it will be difficult to guess what your example really means, so it is best to supply example strings that are not ambiguous. For example the following example will fail if you intend it to be mm/dd/yyyy:

$stamp->stamp("4/4/2012", time());

The reason for this is that the information provided makes it impossible to distinguish between m/d/y or d/y/m since both single digit values are valid for both month or day. To avoid this it is best to supply example days > 12 and example months < 12.

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.