Enabling microseconds in Symfony2 (Doctrine) and MySQL

I have an object with a single column of type "datetime" to store a timestamp.

/** * @ORM\Column(type="datetime") */ protected $timestamp; 

I had MySQL 5.5.40, and I found that it does not store microseconds. So I switched to 5.6.21 and imported all my tables and data.

I tried to declare the type as

  * @ORM\Column(type="datetime(6)") 

but it gave me a mistake. Therefore, I changed it directly to the database by doing:

 ALTER TABLE symfony.hrmgmt MODIFY timestamp DATETIME(6); 

In my controller, I do this:

  $dt = new \DateTime('now'); $newHREvent->setTimestamp($dt); 

Nevertheless, the timestamp is saved without a split second.

I can (now) manually enter datetime with fractional values ​​via SQL, but when I do this through my controller, it always stores from .000000

I believe that since Doctrine does not know that it can also store microseconds.

My version of PHP is still 5.4.34.

Thanks!

+5
source share
1 answer

To create a DateTime::createFromFormat with a microsecond, you must use DateTime::createFromFormat in your controller

 $date = \DateTime::createFromFormat('U.u', (string)microtime(true)); 
+1
source

Source: https://habr.com/ru/post/1209924/


All Articles