cornernote/yii-extended-date-time-formatting-behavior

There is no license information available for the latest version (dev-master) of this package.

[Yii] Behavior for CDateFormater that adds some methods for extending date&time formatting.

dev-master 2014-07-26 03:24 UTC

This package is auto-updated.

Last update: 2024-12-19 19:14:40 UTC


README

ExtendedDateTimeFormattingBehavior adds some methods for extending date&time formatting to CDateFormatter component.

Available formatters:

  • formatDateTimeReadable($timestamp, $dateWidth = 'medium', $timeWidth = 'medium') formats date&time with pattern (Today|Yesterday|<date>), <time>.
  • $dateWidth is passed to CDateFormatter::formatDateTime() to format <date>
  • $timeWidth is passed to CDateFormatter::formatDateTime() to format <time>
  • formatDateTimeInterval($timestamp, $precisely = false) formats date&time as a date&time interval with pattern <metric value> <metric> ago or more complex <first metric value> <first metric> and <second metric value> <second metric> ago
  • $precisely is setted to true, interval will be composed of two metrics.

$timestamp in both formatters can be unix timestamp (integer) or string to pass it to strtotime().

How to use: attach this behavior to your dateFormatter instance.

For example, add this in your base controller class (Controller) and base console command class (ConsoleCommand):

    public function init()
    {
        parent::init();
        Yii::app()->dateFormatter->attachBehavior('ExtendedDateTimeFormatting', 'ext.ExtendedDateTimeFormattingBehavior.ExtendedDateTimeFormattingBehavior');
    }

Example:

echo Yii::app()->dateFormatter->formatDateTimeReadable('yesterday, 20:45:17');

returns Yesterday, 8:45:17 PM20

echo Yii::app()->dateFormatter->formatDateTimeInterval('yesterday, 20:45:17', true);

returns 20 hours and 54 minutes ago