为了账号安全,请及时绑定邮箱和手机立即绑定

Symfony DateTimeType 没有秒数/当天的 TimeType

Symfony DateTimeType 没有秒数/当天的 TimeType

PHP
慕运维8079593 2022-10-09 20:12:00
对于我的项目,我需要多个具有“特殊”配置的 DateTimeTypes - 但我根本不知道如何实现这些东西(Symfony 5)。1. DateTimeType 不带秒(但仍然是 HTML5) $builder     ->add('arrival', DateTimeType::class, ['label' => 'Arrival', 'input' => 'datetime_immutable', 'widget' => 'single_text']);这会呈现一个 HTML5 输入字段,其类型datetime-local正是我想要的 - 除了它仍然有几秒钟:我想删除:46 。这显然来自 symfony 自动设置的 value 选项(?):从技术上讲,这已经在 chrome 等中有效。但是,由于秒数,这在 Safari 中的客户端验证失败。如何删除秒?该format选项不能使用(因为它需要将html5设置为false)!2.当日时间类型如果您将 TimeType 与DateTime对象一起使用,日期将始终是1970-01-01完全有意义的 - 但是您如何预先选择对象的日期(例如今天)并让用户选择时间?我怎样才能做到这一点?
查看完整描述

1 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

为了设置今天的默认值,我construct在我的实体对象中使用该方法。


    public function __construct()

    {

        $this->arrival = new \DateTimeImmutable();

        $this->departure = \new \DateTimeImmutable();

    }

Q1 的答案:为了删除秒,只需创建一个DateTime没有秒的新对象。


    public function __construct()

    {

        $this->arrival = \DateTimeImmutable::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'));

        $this->departure = \DateTimeImmutable::createFromFormat('Y-m-d H:i', date('Y-m-d H:i'));

    }


    //$this->arrival = new \DateTimeImmutable(date('Y-m-d H:i')); //this also works, but needs execption handling (try/catch)

Q2的答案:此问题的答案相同 - 只需在实体中预设当前日期并使用TimeType让用户选择时间。


查看完整回答
反对 回复 2022-10-09
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信