Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling microseconds in Symfony2 (Doctrine) and MySQL

I have an entity with one column of "datetime" type to store a timestamp.

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

I had MySQL 5.5.40 and I discovered 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 an error. So I changed it directly in the DB by doing:

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

In my controller I do this:

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

But nonetheless the timestamp is stored without fractions of second.

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

I suppose that's because Doctrine does not know that it can store also microseconds.

My PHP version is still 5.4.34.

Thank you!

like image 923
Sergio Negri Avatar asked Nov 01 '22 12:11

Sergio Negri


1 Answers

To create a datetime with microsecond you have to use DateTime::createFromFormat in your controller

$date = \DateTime::createFromFormat('U.u', (string)microtime(true));
like image 125
goto Avatar answered Nov 15 '22 06:11

goto