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 ~~~
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.
ok
@cornernote, ok. I restored git repo and composer package available again.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.