在Symfony2(Doctrine)和MySQL中启用微秒 [英] Enabling microseconds in Symfony2 (Doctrine) and MySQL
问题描述
我有一个具有一列datetime类型的实体来存储时间戳。
/ **
* @ ORM\Column(type =datetime)
* /
protected $ timestamp;
我有MySQL 5.5.40,我发现它不存储微秒。所以我切换到5.6.21并导入了我的所有表格和数据。
我尝试将类型声明为
< blockquote>
* @ ORM\Column(type =datetime(6))
但它给了我一个错误。所以我直接在数据库中改变了:
ALTER TABLE symfony.hrmgmt MODIFY timestamp DATETIME (6);
在我的控制器我这样做:
$ dt = new \DateTime('now');
$ newHREvent-> setTimestamp($ dt);
但是,尽管如此,时间戳也不存在秒数。 p>
我可以(现在)通过SQL手动输入具有小数值的datetime,但是当我通过我的控制器进行输入时,它始终以.000000
我想这是因为Doctrine不知道它也可以存储微秒。
我的PHP版本仍然是5.4.34。
谢谢!
要创建一个带有微秒的datetime,您必须使用 DateTime :: createFromFormat
在控制器中
$ date = \DateTime :: createFromFormat('U.u',(string)microtime(true));
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!
To create a datetime with microsecond you have to use DateTime::createFromFormat
in your controller
$date = \DateTime::createFromFormat('U.u', (string)microtime(true));
这篇关于在Symfony2(Doctrine)和MySQL中启用微秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!