How to create a DateTime object from a string in symfony2 / php

In a DB table, I have several fields with datetime as a field type. Therefore, I need to save the data only as an object of time.

From the form, I get the date as a string as

 2012-10-05 17:45:54 

Now, when I save my entity, I get the following error:

Fatal error: a call in the format of a member function () for a non-object in .. \ DateTimeType.php on line 44

I tried using

 $protocol->setStartedAt(strtotime($post['started_at'])); 

or

 $from = \DateTime::createFromFormat('yy-mm-dd hh:mm:ss', $post['started_at']); $protocol->setStartedAt($from); 

or simply

 $from = new \DateTime($post['started_at']); $protocol->setStartedAt($from); 

The latter code works, but it does not use the timestamp passed as an argument, but simply receives the current time.

Any ideas?

+11
source share
4 answers

I always create a DateTime object with its constructor, os in your case it will be:

 $protocol->setStartedAt(new \DateTime($post['started_at'])); 

if this works, but does not use the published timestamp, you probably do not have a value in $ post ['started_at']. Try debugging it or just do the dirty trick:

 die($post['started_at']); 
+17
source

For future readers who will surely ever run into this problem (this is the first post if you google "symfony 2 datetime from string"), keep in mind that in Symfony 2 DateTime an object DOES NOT accept a string with this format: "d/m/YH:i:s" and probably also doesn't support many others.

In order not to become insane , I really found that the simplest and safest solution to avoid such errors is this one:

First, get your string date from any of your request (in my case, a general AJAX request) and convert it to a DateTime Object , this example assumes that we need to create a dateTime object for 25/04/2015 15:00 , which is jQuery UI italian DateTimePicker format (this is just an example):

 $literalTime = \DateTime::createFromFormat("d/m/YH:i","25/04/2015 15:00"); 

(note: use \ to use the php DateTime object, otherwise you will use the symfony datetime object that throws you an exception)

Then, once you have done this, create a date string using the function , specifying the expected output format ( Ymd H:i:s ) as the first parameter:

 $expire_date = $literalTime->format("Ymd H:i:s"); 

That way, you are 100% sure that any format you send or receive will be correctly converted, and you will not receive any exception to the Symfony DateTime object , while you provide what you expect as input .

Knowing that this post is actually quite old, I only decided to publish it because I could not find another valuable source, but this one, in order to understand where the problem might be.

Please note that the best solution is still to send the datetime string in the correct format, but if you literally have no way to do this, the safest way to convert such a string is as described above.

+10
source

How about createFromFormat?

http://uk.php.net/manual/en/datetime.createfromformat.php

 $from = DateTime::createFromFormat($post['started_at'], 'Ymd H:i:s'); 
+1
source

PS: This is not an answer, just another question ...

Well, I know this is an old question, but with the same situation. Now I take the "date" from the form and pass all this data using the form.serialize() method using ajax. On the Symfony side, I get this value in string format. Now, when I try to insert this data into the database, I get an error, because the type of this field is assigned a "date". So thanks to @Laurynas Mališauskas I solved this problem. But what if now I get the null value from the form for this date value? Any ideas? Oh, by the way, now I used the condition if($dateValue != null OR $dateValue != '') . Let me, if there is some other and better option ... Thanks in advance .. :)

0
source

All Articles