conquer / momentjs
Moment.js for Yii2 with the application locale and PHP date format conversion
Requires
- php: ^7.4 || ^8.0
- bower-asset/moment: ^2.9.0
- yiisoft/yii2: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Description
Parse, validate, manipulate, and display dates in JavaScript. For more information please visit Moment.js.
Requirements
- PHP 7.4 or 8.x
- Yii 2.0
- asset-packagist repository configured in your application's
composer.json(standard for Yii2 app templates)
Installation
Install the extension via Composer:
composer require conquer/momentjs:^1.2
Usage
\conquer\momentjs\MomentjsAsset::register($this);
Moment.js is loaded with the locale of Yii::$app->language (ru-RU falls back to ru, English needs no file),
set globally before the inline scripts of the page run.
use conquer\momentjs\MomentjsAsset; MomentjsAsset::$language = 'de'; // another locale MomentjsAsset::$language = false; // no locale MomentjsAsset::$allLocales = true; // all locales (~370 KB), e.g. to call moment.locale() in the browser
PHP date format
MomentjsAsset::fromPhpFormat() converts a PHP date() format
to a Moment.js format:
MomentjsAsset::fromPhpFormat('D, jS F Y H:i'); // "ddd, Do MMMM YYYY HH:mm" MomentjsAsset::fromPhpFormat('Y-m-d\TH:i:sP'); // "YYYY-MM-DD[T]HH:mm:ssZ"
Escaped (\x) and other literal characters are kept. Characters Moment.js cannot output are removed:
z, t, L, B, I, e, T, Z, p, X, x, and S except right after j (jS → Do).
Upgrading to 1.2
- Only the selected locale is loaded (
min/moment.min.js+locale/<lang>.js) instead ofmoment-with-locales.min.js. If you switch the locale in the browser, setMomentjsAsset::$allLocales = true. - The locale is set at
View::POS_END(before the other inline scripts) instead of on document ready. sourcePathis@bower/moment(was@bower/moment/min); onlymin/andlocale/are published.fromPhpFormat()output changed for the formats it converted wrongly (D,l,S,N,w,W, escapes): re-check stored or hard-coded converted formats.
License
conquer/momentjs is released under the MIT License. See the bundled LICENSE.md for details.