php Symfony2 datetime 存储时间戳的最佳方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11504997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 datetime best way to store timestamps?
提问by nasy
I don't know which is the best way to store a timestamp in the database. I want to store the entire date with hours minutes and seconds but it only stores the date ( for instance 2012-07-14 ) and i want to store 2012-07-14 HH:MM:SS. I am using the dateTime object. Here is the code:
我不知道在数据库中存储时间戳的最佳方式是什么。我想用小时分和秒存储整个日期,但它只存储日期(例如 2012-07-14 ),我想存储 2012-07-14 HH:MM:SS。我正在使用 dateTime 对象。这是代码:
In the controller:
在控制器中:
$user->setCreated(new \DateTime());
In the entity:
在实体中:
/**
* @var date $created
*
* @ORM\Column(name="created", type="date")
*/
private $created;
Is it better to store the date and the the time separately in the database ? or better to store all together like YYYY-MM-DD HH:MM:SS ? I will have then to compare dates and calculate the remaining times, so that is important in order to simplify the operations later. So what do you think ? can somebody help me?
将日期和时间分别存储在数据库中是否更好?或者更好地像 YYYY-MM-DD HH:MM:SS 一样存储在一起?然后我将不得不比较日期并计算剩余时间,这对于简化以后的操作很重要。所以你怎么看 ?有人可以帮我吗?
采纳答案by Gordon
The best way to store a timestamp in the database is obviously to use the timestamp column if your database supports that type. And since you can set that column to autoupdate on create, you dont even have to provide a setter for it.
在数据库中存储时间戳的最佳方法显然是使用时间戳列,如果您的数据库支持该类型。并且由于您可以将该列设置为在创建时自动更新,您甚至不必为其提供设置器。
There is a Timestampable behavior extension for Doctrine 2which does exactly that from the userland side as well:
Doctrine 2有一个Timestampable 行为扩展,它也从用户端方面做到了这一点:
Timestampable behavior will automate the update of date fields on your Entities or Documents. It works through annotations and can update fields on creation, update or even on specific property value change.
Features:
- Automatic predifined date field update on creation, update and even on record property changes
- ORM and ODM support using same listener
- Specific annotations for properties, and no interface required
- Can react to specific property or relation changes to specific value
- Can be nested with other behaviors
- Annotation, Yaml and Xml mapping support for extensions
时间戳行为将自动更新实体或文档上的日期字段。它通过注释工作,可以在创建、更新甚至特定属性值更改时更新字段。
特征:
- 在创建、更新甚至记录属性更改时自动更新预定义日期字段
- ORM 和 ODM 支持使用相同的侦听器
- 属性的特定注释,不需要接口
- 可以对特定属性或特定值的关系变化做出反应
- 可以与其他行为嵌套
- 对扩展的注解、Yaml 和 Xml 映射支持
With this behavior, all you need to do is change your annotation to
有了这种行为,您需要做的就是将注释更改为
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
Then you dont need to call setCreatedin your code. The field will be set automatically when the Entity is created for the first time.
然后你不需要调用setCreated你的代码。该字段将在第一次创建实体时自动设置。
回答by Mick
In order to store the date of creation without using the Timestampable behaviour of doctrine, you can also use LifeCycle Callbacks, by adding the annotation @ORM\HasLifecycleCallbackswhen you declare the class. Here is what would work in your case to store YYYY-MM-DD HH:MM:SSin the database.
为了在不使用 Timestampable 行为的情况下存储创建日期,您还可以使用LifeCycle Callbacks,通过@ORM\HasLifecycleCallbacks在声明类时添加注释。以下是在您的情况下将YYYY-MM-DD HH:MM:SS 存储在数据库中的方法。
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="yourTable")
*/
class Nasy
{
/**
* @ORM\Column(name="created", type="string", length=255)
*/
private $created;
/**
* @ORM\PrePersist
*/
public function doStuffOnPrePersist()
{
$this->created = date('Y-m-d H:i:s');
}
Finally, if you have a problem of timezone, you could set the timezone in the session by using an event listeneron login. Matt Drolette did an awesome work on his blog here. You will probably always be storing the time in the timezone your server is in anyway. Then you use the timezone set in the session to display the right time to the user. Good luck.
最后,如果您有时区问题,您可以在登录时使用事件侦听器在会话中设置时区。马特Drolette做了自己的博客真棒工作在这里。无论如何,您可能总是将时间存储在您的服务器所在的时区。然后使用会话中设置的时区向用户显示正确的时间。祝你好运。
回答by thenetimp
Building on @Pratt's answer I did this. I have 2 fields in my entities one for created and one for modified.
基于@Pratt 的回答,我做到了。我的实体中有 2 个字段,一个用于创建,一个用于修改。
/**
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @ORM\Column(type="datetime")
*/
protected $modified_at;
And then using annotation I call this on prePersist and preUpdate
然后使用注释我在 prePersist 和 preUpdate 上调用它
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setModifiedAt(new \DateTime(date('Y-m-d H:i:s')));
if($this->getCreatedAt() == null)
{
$this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
}
}
The function could be broken up into 2 functions one for create one for update, but this is working so I see no reason for the extra code when this is working properly.
该函数可以分解为 2 个函数,一个用于创建一个用于更新,但这是有效的,因此当它正常工作时,我认为没有理由添加额外的代码。
回答by amdramdas
You can use @Version like this:
你可以像这样使用@Version:
/**
* @var date $created
*
* @ORM\Column(name="created", type="datetime")
* @ORM\Version
*/
private $created;
This will only work on a datetime type.
这仅适用于日期时间类型。

