Download the PHP package mducharme/date-formatter without Composer
On this page you can find all versions of the php package mducharme/date-formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mducharme/date-formatter
More information about mducharme/date-formatter
Files in mducharme/date-formatter
Package date-formatter
Short Description A date formatter to easily standardize date-time values in a PHP application / API.
License MIT
Informations about the package date-formatter
Date Formatter
A simple service to format a date in constant format(s) across a PHP project / API.
Table of contents
- How to install
- How to use
- The parser
- The formatter
- The Service Provider
How to install
PHP 7 is required. Install with composer:
How to use
Ready-to-use, with a Pimple container:
With the services directly:
The parser
In addition to the Formatter
, a Parser
service is also included. It has a single purpose, to ensure a mixed value, either a DateTime object or a parsable string, is parsed a DateTime object.
Any invalid string or parameter will throw an exception (\InvalidArgumentException
) when parsing.
Except null value, which are allowed by default but may be disallowed with a parameter to the parse()
method. In thoses cases, null
will be returned by the parse()
method instead of a \DateTimeInterface
object if it is allowed as a parameter.
Handling null
:
As a single-purpose service, the Parser
is also invokable and may be called directly:
The Formatter
The formatter is the main Service provided by this library. It has a single purpose, to render a DateTime object into a formatted string (or multiple formats).
As a single-purpose service, the Formatter
is also invokable:
It comes with many formats by default. See the Formatter source file for details on the default format. You may get an array of all available formats by using Formatter::ALL
as the format parameter:
It is possible to add custom formats, or overwrite default ones, by passing an optional array of formats to the Formatter constructor.
It is also possible to return an array of formatted dates, by specifying an array of formats:
Formats can either be a string, which will be formatted with DateTimeInterface::format()
or a callback function with the following signature:
If no $format
is provided to the format()
method (2nd parameter), then the default one will be used. Setting the default format is possible with the constructor. It is optional and will default to the 'atom' format.
The Service Provider
As a convenience, a Pimple Service Provider is also included for an already bootstrapped parser (date/parser
) and formatter (date/formatter
).
To customize the options, the date/custom-formats
and date/default-format
container options can be extended.