| 1 |
[![Latest Stable Version]](https://packagist.org/packages/wikimedia/timestamp) [![License]](https://packagist.org/packages/wikimedia/timestamp) |
| 2 |
|
| 3 |
Convertible Timestamp for PHP |
| 4 |
=========================== |
| 5 |
|
| 6 |
This library provides a convenient wrapper around DateTime to |
| 7 |
create, parse, and format timestamps. |
| 8 |
|
| 9 |
Additional documentation about the library can be found on |
| 10 |
[MediaWiki.org](https://www.mediawiki.org/wiki/Timestamp). |
| 11 |
|
| 12 |
Supported formats |
| 13 |
----------------- |
| 14 |
|
| 15 |
On input, the following formats are recognized: |
| 16 |
|
| 17 |
* A subset of [ISO 8601] basic and extended formats: |
| 18 |
* Years before 0000 or after 9999 are not supported. |
| 19 |
* Week and ordinal dates are not supported. |
| 20 |
* Accuracy to seconds is required. Fractions of a second are supported to microsecond resolution. |
| 21 |
* If the timezone is omitted, UTC is assumed. |
| 22 |
* As an extension, the 'T' may be replaced with a single space. |
| 23 |
* As a signed integer (up to 13 digits) representing seconds since the Unix epoch. |
| 24 |
* Optionally with decimal seconds to microsecond resolution, using '.' as the decimal separator. |
| 25 |
* [RFC 2822] format, including obsolete syntax. |
| 26 |
* CFWS tokens are not fully supported, use only FWS. |
| 27 |
* Note, per the RFC, all military timezones are considered as -0000. |
| 28 |
* As an extension, the timezone may be omitted entirely in which case UTC is assumed. |
| 29 |
* As an extension, anything after the first semicolon in the string is ignored. |
| 30 |
* [RFC 850] format. |
| 31 |
* [asctime] format. |
| 32 |
* The `TS_MW`, `TS_DB`, `TS_POSTGRES`, `TS_ORACLE`, and `TS_EXIF` formats described below. |
| 33 |
|
| 34 |
For output, the following conversions are predefined: |
| 35 |
|
| 36 |
* `TS_DB`: MySQL datetime format: "2012-07-31 19:01:08" |
| 37 |
* `TS_EXIF`: Exif 2.2 format: "2012:07:31 19:01:08" |
| 38 |
* `TS_ISO_8601`: [ISO 8601] expanded format: "2012-07-31T19:01:08Z" |
| 39 |
* `TS_ISO_8601_BASIC`: [ISO 8601] basic format: "20120731T190108Z" |
| 40 |
* `TS_MW`: A 14-digit string: "20120731190108" |
| 41 |
* `TS_ORACLE`: A default Oracle timestamp format: "31-07-2012 19:01:08.000000" |
| 42 |
* `TS_POSTGRES`: PostgreSQL default timestamptz format: "2012-07-31 19:01:08+00" |
| 43 |
* `TS_RFC2822`: [RFC 2822] format using an obsolete timezone: "Tue, 31 Jul 2012 19:01:08 GMT" |
| 44 |
* `TS_UNIX`: Seconds since the Unix epoch (1970-01-01T00:00:00Z): "1343761268". |
| 45 |
* `TS_UNIX_MICRO`: Seconds since the Unix epoch with microseconds: "1343761268.000000". |
| 46 |
|
| 47 |
Usage |
| 48 |
----- |
| 49 |
|
| 50 |
```php |
| 51 |
$ts = new ConvertibleTimestamp( '2012-07-31T19:01:08Z' ); |
| 52 |
$formatted = $ts->getTimestamp( TS_UNIX ); |
| 53 |
|
| 54 |
// Shorthand |
| 55 |
$formatted = ConvertibleTimestamp::convert( |
| 56 |
TS_UNIX, '2012-07-31T19:01:08Z' |
| 57 |
); |
| 58 |
|
| 59 |
// Format using PHP date formatting codes |
| 60 |
$formatted = $ts->format( 'Y-m-d H:i:s O' ); |
| 61 |
``` |
| 62 |
|
| 63 |
|
| 64 |
Running tests |
| 65 |
------------- |
| 66 |
|
| 67 |
composer install --prefer-dist |
| 68 |
composer test |
| 69 |
|
| 70 |
|
| 71 |
--- |
| 72 |
[Latest Stable Version]: https://poser.pugx.org/wikimedia/timestamp/v/stable.svg |
| 73 |
[License]: https://poser.pugx.org/wikimedia/timestamp/license.svg |
| 74 |
[ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 |
| 75 |
[RFC 2822]: https://www.rfc-editor.org/rfc/rfc2822.html#section-3.3 |
| 76 |
[RFC 850]: https://www.rfc-editor.org/rfc/rfc850.html#section-2.1.4 |
| 77 |
[asctime]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/asctime.html |