extended-date-time-formatting

Behavior for CDateFormater that adds some methods for extending date&time formatting.
5 followers

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

Formatters

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().


Usage

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

For example, add this in your base controller class (Controller):

public function beforeAction($action) {
    Yii::import('ext.ExtendedDateTimeFormattingBehavior.ExtendedDateTimeFormattingBehavior');
        Yii::app()->dateFormatter->attachBehavior('ExtendedDateTimeFormatting', new ExtendedDateTimeFormattingBehavior());
        return parent::beforeAction($action);
    }

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

Resources

Total 1 comment

#17826 report it
cornernote at 2014/07/25 11:17pm
git link broken

Hello,

Seems the git project page has been removed. Any chance you can put it back?

It's still in the packagist.org (composer) directory.

I added my own so I can get it with composer:

https://github.com/cornernote/yii-extended-date-time-formatting-behavior

https://packagist.org/packages/cornernote/yii-extended-date-time-formatting-behavior

Once you put yours back online let me know and I'll remove mine.

Leave a comment

Please to leave your comment.

Create extension