Symfony2 datetime存储时间戳的最佳方式是什么?

时间:2022-10-22 17:08:30

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?

是否最好将日期和时间分别存储在数据库中?还是像yyyyy -MM- dd - HH:MM:SS ?然后我将不得不比较日期并计算剩余的时间,所以这对于以后简化操作是很重要的。你怎么看?有人能帮助我吗?

4 个解决方案

#1


12  

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.

在数据库中存储时间戳的最佳方法显然是,如果您的数据库支持该类型,则使用时间戳列。既然您可以在create上将该列设置为autoupdate,您甚至不必为它提供一个setter。

There is a Timestampable behavior extension for Doctrine 2 which does exactly that from the userland side as well:

第2条有一个时间可识别的行为扩展,它也从用户端做同样的事情:

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
  • ORM和ODM支持使用相同的侦听器
  • 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
  • 注释、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 setCreated in your code. The field will be set automatically when the Entity is created for the first time.

那么您就不需要在代码中调用setCreated。当第一次创建实体时,将自动设置字段。

#2


11  

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\HasLifecycleCallbacks when you declare the class. Here is what would work in your case to store YYYY-MM-DD HH:MM:SS in the database.

为了存储创建日期而不使用doctrine的时间标记行为,还可以使用生命周期回调,方法是在声明类时添加注释@ORM\HasLifecycleCallbacks。以下是在您的情况下可以在数据库中存储yyyyyy -MM- dd: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 listener on 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.

最后,如果您遇到时区问题,可以在登录时使用事件监听器在会话中设置时区。马特·德罗莱特在他的博客上写了一篇很棒的文章。无论如何,您可能总是将时间存储在服务器所在的时区。然后,您将使用会话中的时区设置来显示用户的正确时间。祝你好运。

#3


5  

Building on @Pratt's answer I did this. I have 2 fields in my entities one for created and one for modified.

基于@Pratt的回答,我这样做了。我的实体中有两个字段,一个用于创建,一个用于修改。

/**
* @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

然后使用annotation,我把它命名为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.

这个函数可以分解成两个函数,一个用于创建一个用于更新,但是这是有效的,所以当它正常工作的时候,我看不出为什么要添加额外的代码。

#4


3  

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.

这只适用于datetime类型。

#1


12  

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.

在数据库中存储时间戳的最佳方法显然是,如果您的数据库支持该类型,则使用时间戳列。既然您可以在create上将该列设置为autoupdate,您甚至不必为它提供一个setter。

There is a Timestampable behavior extension for Doctrine 2 which does exactly that from the userland side as well:

第2条有一个时间可识别的行为扩展,它也从用户端做同样的事情:

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
  • ORM和ODM支持使用相同的侦听器
  • 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
  • 注释、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 setCreated in your code. The field will be set automatically when the Entity is created for the first time.

那么您就不需要在代码中调用setCreated。当第一次创建实体时,将自动设置字段。

#2


11  

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\HasLifecycleCallbacks when you declare the class. Here is what would work in your case to store YYYY-MM-DD HH:MM:SS in the database.

为了存储创建日期而不使用doctrine的时间标记行为,还可以使用生命周期回调,方法是在声明类时添加注释@ORM\HasLifecycleCallbacks。以下是在您的情况下可以在数据库中存储yyyyyy -MM- dd: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 listener on 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.

最后,如果您遇到时区问题,可以在登录时使用事件监听器在会话中设置时区。马特·德罗莱特在他的博客上写了一篇很棒的文章。无论如何,您可能总是将时间存储在服务器所在的时区。然后,您将使用会话中的时区设置来显示用户的正确时间。祝你好运。

#3


5  

Building on @Pratt's answer I did this. I have 2 fields in my entities one for created and one for modified.

基于@Pratt的回答,我这样做了。我的实体中有两个字段,一个用于创建,一个用于修改。

/**
* @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

然后使用annotation,我把它命名为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.

这个函数可以分解成两个函数,一个用于创建一个用于更新,但是这是有效的,所以当它正常工作的时候,我看不出为什么要添加额外的代码。

#4


3  

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.

这只适用于datetime类型。