Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cornernote/extendeddatetimeformattingbehavior
[Yii] Behavior for CDateFormater that adds some methods for extending date&time formatting.
https://github.com/cornernote/extendeddatetimeformattingbehavior
Last synced: about 2 months ago
JSON representation
[Yii] Behavior for CDateFormater that adds some methods for extending date&time formatting.
- Host: GitHub
- URL: https://github.com/cornernote/extendeddatetimeformattingbehavior
- Owner: cornernote
- Created: 2014-04-21T06:10:57.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-04-21T23:53:57.000Z (over 10 years ago)
- Last Synced: 2024-10-15T14:29:50.597Z (3 months ago)
- Language: PHP
- Homepage:
- Size: 144 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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`):
```php
public function beforeAction($action) {
Yii::import('ext.ExtendedDateTimeFormattingBehavior.ExtendedDateTimeFormattingBehavior');
Yii::app()->dateFormatter->attachBehavior('ExtendedDateTimeFormatting', new ExtendedDateTimeFormattingBehavior());
return parent::beforeAction($action);
}
```
---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**