Search by

conquer / momentjs

borodulin

Moment.js for Yii2 with the application locale and PHP date format conversion

Package info

github.com/borodulin/yii2-momentjs

Type:yii2-extension

pkg:composer/conquer/momentjs

Statistics

Installs: 127 881

Dependents: 4

Suggesters: 0

Stars: 2

Open Issues: 0

1.2.0 2026-11-07 02:15 UTC

This package is auto-updated.

Last update: 2026-11-07 02:17:15 UTC


README

Latest Version Total Downloads CI License

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 of moment-with-locales.min.js. If you switch the locale in the browser, set MomentjsAsset::$allLocales = true.
  • The locale is set at View::POS_END (before the other inline scripts) instead of on document ready.
  • sourcePath is @bower/moment (was @bower/moment/min); only min/ and locale/ 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.