2014-10-19 10:32:34 +02:00
|
|
|
title: DateField
|
|
|
|
summary: How to format and use the DateField class.
|
|
|
|
|
|
|
|
# DateField
|
|
|
|
|
2017-02-14 06:19:09 +01:00
|
|
|
This `FormField` subclass lets you display an editable date, in a single text input field.
|
|
|
|
It also provides a calendar date picker.
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
The following example will add a simple DateField to your Page, allowing you to enter a date manually.
|
|
|
|
|
|
|
|
**mysite/code/Page.php**
|
|
|
|
|
|
|
|
:::php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
class Page extends SiteTree {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'MyDate' => 'Date',
|
|
|
|
);
|
|
|
|
|
|
|
|
public function getCMSFields() {
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DateField::create('MyDate', 'Enter a date')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## Custom Date Format
|
|
|
|
|
2017-02-14 06:19:09 +01:00
|
|
|
A custom date format for a [api:DateField] can be provided through `setDateFormat`.
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
:::php
|
|
|
|
// will display a date in the following format: 31-06-2012
|
2017-01-26 05:20:08 +01:00
|
|
|
DateField::create('MyDate')->setDateFormat('dd-MM-yyyy');
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
<div class="info" markdown="1">
|
2017-03-30 23:37:21 +02:00
|
|
|
The formats are based on [ICU format](http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details).
|
2014-10-19 10:32:34 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
## Min and Max Dates
|
|
|
|
|
|
|
|
Sets the minimum and maximum allowed date values using the `min` and `max` configuration settings (in ISO format or
|
2017-03-30 23:37:21 +02:00
|
|
|
`strtotime()`).
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
:::php
|
|
|
|
DateField::create('MyDate')
|
2017-02-14 06:19:09 +01:00
|
|
|
->setMinDate('-7 days')
|
2017-03-30 23:37:21 +02:00
|
|
|
->setMaxDate('2012-12-31')
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
## Separate Day / Month / Year Fields
|
|
|
|
|
2017-02-15 20:26:51 +01:00
|
|
|
To display separate input fields for day, month and year separately you can use the `SeparatedDateField` subclass`.
|
2017-02-14 06:19:09 +01:00
|
|
|
HTML5 placeholders 'day', 'month' and 'year' are enabled by default.
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
:::php
|
2017-02-15 20:26:51 +01:00
|
|
|
SeparatedDateField::create('MyDate');
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
<div class="alert" markdown="1">
|
|
|
|
Any custom date format settings will be ignored.
|
|
|
|
</div>
|
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
## Date Picker and HTML5 support
|
2014-10-19 10:32:34 +02:00
|
|
|
|
2017-03-30 23:37:21 +02:00
|
|
|
The field can be used as a [HTML5 input date type](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date)
|
|
|
|
(with `type=date`) by calling `setHTML5(true)`.
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
:::php
|
|
|
|
DateField::create('MyDate')
|
2017-03-30 23:37:21 +02:00
|
|
|
->setHTML5(true);
|
|
|
|
|
|
|
|
In browsers [supporting HTML5 date inputs](caniuse.com/#feat=input-datetime),
|
|
|
|
this will cause a localised date picker to appear for users.
|
|
|
|
In this mode, the field will be forced to present and save ISO 8601 date formats (`y-MM-dd`),
|
|
|
|
since the browser takes care of converting to/from a localised presentation.
|
|
|
|
|
|
|
|
Browsers without support receive an `<input type=text>` based polyfill.
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
## Formatting Hints
|
|
|
|
|
|
|
|
It's often not immediate apparent which format a field accepts, and showing the technical format (e.g. `HH:mm:ss`) is
|
|
|
|
of limited use to the average user. An alternative is to show the current date in the desired format alongside the
|
|
|
|
field description as an example.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$dateField = DateField::create('MyDate');
|
|
|
|
|
|
|
|
// Show long format as text below the field
|
2017-02-14 06:19:09 +01:00
|
|
|
$dateField->setDescription(_t(
|
|
|
|
'FormField.Example',
|
|
|
|
'e.g. {format}',
|
|
|
|
[ 'format' => $dateField->getDateFormat() ]
|
2014-10-19 10:32:34 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
// Alternatively, set short format as a placeholder in the field
|
2017-02-14 06:19:09 +01:00
|
|
|
$dateField->setAttribute('placeholder', $dateField->getDateFormat());
|
2014-10-19 10:32:34 +02:00
|
|
|
|
|
|
|
<div class="notice" markdown="1">
|
2016-02-17 22:21:59 +01:00
|
|
|
Fields scaffolded through [api:DataObject::scaffoldCMSFields()] automatically have a description attached to them.
|
2014-10-19 10:32:34 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
## API Documentation
|
|
|
|
|
2016-02-17 22:21:59 +01:00
|
|
|
* [api:DateField]
|