跳到内容

EasyAdmin DateTime Field

编辑此页

此字段用于表示存储 PHP DateTimeInterface 值(例如 DateTime, DateTimeImmutable 等)的值。

表单页面 (编辑和新建) 中,它看起来像这样

Default style of EasyAdmin datetime field

基本信息

  • PHP 类: EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField
  • Doctrine DBAL 类型 用于存储此值: datetime, datetime_immutable datetimetzdatetimetz_immutable
  • Symfony 表单类型 用于渲染字段: DateTimeType
  • 渲染为:

    1
    <input type="datetime-local"> ... </select>

选项

renderAsChoice

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使它们更易于使用。

如果您希望将日期/时间显示为 5 个独立的 <select> 元素,以分别选择日、月、年、小时和分钟,请使用此选项

1
yield DateTimeField::new('...')->renderAsChoice();

注意

设置此选项等同于在底层的 DateType Symfony 字段中设置 widget = choicehtml5 = true 选项。

renderAsNativeWidget

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使它们更易于使用。

此选项允许您以编程方式启用/禁用此行为(例如,基于某些表达式的结果)。将其设置为 false 等同于调用 renderAsChoice()

1
yield DateTimeField::new('...')->renderAsNativeWidget(false);

注意

设置此选项等同于在底层的 DateType Symfony 字段中设置 widget = single_texthtml5 = true 选项。

renderAsText

默认情况下,在表单页面(editnew)中,该字段渲染为 HTML5 输入字段。这样做是因为现代浏览器为这些字段显示高级日期选择器,使它们更易于使用。

如果您希望将日期/时间显示为单个 <input type="text"> 元素,请使用此选项

1
yield DateTimeField::new('...')->renderAsText();

注意

设置此选项等同于在底层的 DateType Symfony 字段中设置 widget = single_texthtml5 = false 选项。

setFormat

默认情况下,在只读页面(indexdetail)中,日期/时间以 setDateTimeFormat() CRUD 选项定义的格式显示。使用此选项可覆盖该默认格式

1
2
3
4
5
6
7
// these are the predefined formats: 'short', 'medium', 'long', 'full'
yield DateTimeField::new('...')->setFormat('long', 'none');

// predefined formats are available as constants too
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;

yield DateTimeField::new('...')->setFormat(DateTimeField::FORMAT_LONG, DateTimeField::FORMAT_NONE);

除了预定义的格式外,您还可以通过将有效的 ICU 日期时间模式传递给此函数来配置您自己的格式

1
2
yield DateTimeField::new('...')->setFormat('yyyy.MM.dd G 'at' HH:mm:ss zzz');
yield DateTimeField::new('...')->setFormat('yyyyy.MMMM.dd GGG hh:mm aaa');

setTimezone

默认情况下,在只读页面(indexdetail)中,日期使用 setTimezone() CRUD 选项定义的时区显示。使用此选项可覆盖该默认时区(参数必须是任何有效的 PHP 时区 ID

1
yield DateTimeField::new('...')->setTimezone('Asia/Manila');
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本