extended-date-time-formatting Behavior for CDateFormater that adds some methods for extending date&time formatting.

  1. Formatters
  2. Usage
  3. Example
  4. Resources

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

Also it available on packagist: wapmorgan/yii-extended-date-time-formatting-behavior

Formatters

Available formatters:

  • formatDateTimeReadable()
  • formatDateTimeInterval()
  • formatDateTimeIntervalWide()

Detailed description:

  • 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.
  • formatDateTimeIntervalWide($datetime) formats date&time as an interval with pattern " years, months, days, hours, minutes and seconds ago".

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

Usage

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

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

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

Example

Examples:

echo Yii::app()->dateFormatter->formatDateTimeInterval('today');
echo Yii::app()->dateFormatter->formatDateTimeInterval('tomorrow');
echo Yii::app()->dateFormatter->formatDateTimeInterval('yesterday');

returns ~~~ Today 00:00:00 am Tomorrow 00:00:00 am Yesterday 00:00:00 am ~~~

echo Yii::app()->dateFormatter->formatDateTimeInterval('now');
echo Yii::app()->dateFormatter->formatDateTimeInterval('today');
echo Yii::app()->dateFormatter->formatDateTimeInterval('midnight');
echo Yii::app()->dateFormatter->formatDateTimeInterval('noon');
echo Yii::app()->dateFormatter->formatDateTimeInterval('tomorrow');
echo Yii::app()->dateFormatter->formatDateTimeInterval('yesterday');
echo Yii::app()->dateFormatter->formatDateTimeInterval('-1 year');

returns ~~~ just now 5 hours ago 5 hours ago in 6 hours in 18 hours 1 day ago 1 year ago ~~~

echo Yii::app()->dateFormatter->formatDateTimeIntervalWide('1941-06-22 04:00');
echo Yii::app()->dateFormatter->formatDateTimeIntervalWide('2038-01-19 03:14:08');

returns ~~~ 73 years, 2 months, 7 days, 50 minutes and 56 seconds ago in 23 years, 4 months, 20 days, 21 hours, 23 minutes and 12 seconds ~~~

Resources

3 0
6 followers
730 downloads
Yii Version: 1.1
License: (not set)
Category: Date and Time
Developed by: wapmorgan
Created on: Dec 1, 2013
Last updated: 5 years ago

Downloads

show all

Related Extensions