Jonny White
Jonny White

Reputation: 875

Unix timestamp input for Symfony3 form

I have an Entity in Symfony3 which has a DateTime property called "checkin_at".

I would like to create a form where the input for checkin_at is a unix timestamp and it stores the corresponding DateTime for that timestamp.

E.g. I would enter 1498867201 and it would store 2017-07-01 00:00:01 i the entity.

I can't see how to get the DateTimeType field to accept a UNIX timestamp as a valid entry, or I can't figure out how to get the form to automatically convert NumberType to a DateTime for the entity.

Please advise :)

Upvotes: 0

Views: 528

Answers (1)

Confidence
Confidence

Reputation: 2303

How about creating a setter for the checkin_at variable?

in your entity you would add:

public function setCheckinAt(string $checkinAt){
 $this->checkinAt=date("Y-m-d H:m:s", $checkinAt);
}

you might refine your setter with more verification with Regex or use Symfony\Component\Validator\Constraints as Assert; if you like

Upvotes: 2

Related Questions