Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

ExtendedDateTimeFormattingBehavior adds some methods for extending date&time formatting to CDateFormatter component.
https://github.com/cornernote/yii-extended-date-time-formatting-behavior

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

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`):
```php
public function init()
{
parent::init();
Yii::app()->dateFormatter->attachBehavior('ExtendedDateTimeFormatting', 'ext.ExtendedDateTimeFormattingBehavior.ExtendedDateTimeFormattingBehavior');
}
```
---

Example:
```php
echo Yii::app()->dateFormatter->formatDateTimeReadable('yesterday, 20:45:17');
```
returns
**Yesterday, 8:45:17 PM20**
```php
echo Yii::app()->dateFormatter->formatDateTimeInterval('yesterday, 20:45:17', true);
```
returns
**20 hours and 54 minutes ago**