简体   繁体   中英

Symfony form submission DateTime format

I'm using symfony/twig to generate a simple form to create a blog post. I'm trying to use doctrine to save the date of the post submission. My problem is, I don't know how to get the current date into an acceptable format.

My entity has this:

public function setSubtime(\DateTimeInterface $subtime): self
    {
        $this->subtime = $subtime;

        return $this;
    }

and I'm trying to do this:

$date = date('H:i:s \O\n d/m/Y');
$post->setSubtime($date);

but this throws the App\\Entity\\Post::setSubtime() must implement interface DateTimeInterface error.

So my question is, what kind of date format will it accept?

To clarify what I'm trying to do: I'm trying to get the current date (not time, just date) at the time of post submission and then save it to the "Post" database table. My "Subtime" column is of type DateTime .

The first argument of setSubtime must be an instance of DateTimeInterface.

The date function returns a formatted date string. So I think you should convert it to DateTimeInterface .

Or use the DateTime class:

$post->setSubtime(new DateTime('H:i:s \O\n d/m/Y'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM