1. Go to this page and download the library: Download sjaakp/yii2-fuzzydate library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sjaakp / yii2-fuzzydate example snippets
// December 3, 2008
$born = [
'y' => 2008,
'm' => 12,
'd' => 3
];
// December, 2008
$born = [
'y' => 2008,
'm' => 12,
'd' => null
];
// 2008
$born = [
'y' => 2008,
'm' => null,
'd' => null
];
## Yii2 Fuzzydate classes ##
**Yii2 Fuzzydate** consists of three classes to work with fuzzy dates:
- **FuzzyDateBehavior** Model/ActiveRecord behavior that converts from database to PHP format, and vice versa;
- **DatePicker** Input widget for a fuzzy date;
- **Formatter** Extends from `yii\i18n\Formatter`, and adds a fuzzy date formatting function.
**DatePickerAsset** is a helper class for **DatePicker**.
A demonstration of the **Yii2 Fuzzydate** suit is [here](http://www.sjaakpriester.nl/software/fuzzydate).
## Installation ##
The preferred way to install **Yii2 Fuzzydate** is through [Composer](https://getcomposer.org/). Either add the following to the }
After that, class `Hero` has two 'virtual attributes', `'born'`, and `'died'`.
**Notice:** Don't forget to declare the attributes *safe* in the method `rules()`.
The behavior adds virtual fuzzy date attributes to the model class, which can be read from and written to like normal attributes. The underlying attributes (like `'born1'`, `'born2'`) are accessible as well.
### Attributes ###
#### $attributes ####
Array of names of the fuzzy date attributes.
#### $format ####
Format string to format the underlying 'real' dates with PHP DateTime.
Default is MySQL's DATE format: `'Y-m-d'`. If you use MySQL, there is no reason to change this.
## Formatter ##
This class extends Yii's standard formatter [**yii\i18n\Formatter**](http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html "Yii 2.0 API").
Use this by setting it as component in the application's configuration file (often `config/web.php` or `config/main.php`) like:
// ...
$config = [
// ... lots of other configurations ...
'components' => [
// ... other components ...
'formatter' => [
'class' => 'sjaakp\fuzzydate\Formatter'
],
/// ..
],
// ...
];
// ...
Another way is using:
Yii::$app->set('formatter', 'sjaakp\fuzzydate\Formatter');
After that, `'fuzzyDate'` is just another formatting option, like `'text'` or `'html'`. You can use it in **GridView**, **ListView** or **DetailView** in the following way:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'date:fuzzyDate',
// ...
],
])
$formattedFuzzyDate = Yii::$app->formatter->asFuzzyDate($model->date, 'full');
You get the same result with:
$formattedFuzzyDate = Yii::$app->formatter->format($model->date, [ 'fuzzyDate', 'full' ]);
The text `'fuzzyDate'` is case independent. `'fuzzydate'` works as well.
### Attributes ###
#### $fuzzyDateFormat ####
- if string: `'short'`, `'medium'`, `'long'`, or `'full'`. **Formatter** tries to figure out the formatting of a fuzzy date based on the formatting of a standard date. With most locales, this works OK, however with some locales the results are less satisfying.
- if array: the keys are `'full'`, `'month'`, and `'year'`, corresponding
to the granularity of the fuzzy date. The values are
[ICU date formats](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table "ICU user guide"). Example:
[
'full' => 'MM/dd/yyyy',
'month' => 'MM/yyyy',
'year' => 'yyyy'
]
The default value for **$fuzzyDateFormat** is `'medium'`.
### Methods ###
#### asFuzzyDate ####
public function asFuzzyDate($value, $format = null)
Formats a fuzzy date, as delivered by a Model with a **FuzzyDateBehavior**.
- **$value**: the fuzzy date to format;
- **$format**: can have the same values as **$fuzzyDateFormat**; if `null` (default), `$fuzzyDateFormat` is used;
- **return**: the formatted fuzzy date.
## DatePicker ##
This is an input widget to handle fuzzy dates. It consists of a spin control for the year, a dropdown list for the month, a datepicker for the day, and optionally a 'Today'-button. Both month and day can be blank, indicating an incomplete date.
**DatePicker** has all the attributes and methods of an [**InputWidget**](http://www.yiiframework.com/doc-2.0/yii-widgets-inputwidget.html "Yii 2.0 API") plus the following.
### Attributes ###
#### $minYear, $maxYear ####
Minimum and maximum year of the year spin control. If `null` (default), it is set to the current year. You would normally set at least one of these values to something other than `null`.
#### $controlClass ####
Set this to the (*Bootstrap*) class(es) for the control elements. Example: for large elements you may set this to `'form-control input-lg'`. Default is `'form-control'`.
#### $monthFormat ####
Sets the format of the month names in the dropdown list.
- `'LLLL'` (default) long name
- `'LLL'` short name
- `'LL'` two digits
These are [ICU format strings](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table "ICU user guide").
#### $today ####
Options for the 'Today'-button.
- If `string`: the text for the 'Today'-button. This will not be HTML-encoded before it's rendered.
- If `array`: HTML options for the 'Today'-button. The array value with the key `'content'` will be displayed as text and will not be HTML-encoded; other options will.
- If `null`: no 'Today'-button is rendered.
Default: `'Today'`.
### Usage ###
Use **DatePicker** like any other InputWidget. Example:
<?= $form->field($model, 'date')->widget(DatePicker::class, [
'minYear' => 1970,
])
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.