extended-date-time-formatting

Behavior for CDateFormater that adds some methods for extending date&time formatting.
4 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

Be the first person to leave a comment

Please to leave your comment.

Create extension