Laravel时间戳将时间节省为Unix时间戳 [英] Laravel timestamp saving time as unix timestamp

查看:209
本文介绍了Laravel时间戳将时间节省为Unix时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在laravel中进行更改以将数据库中的时间戳保存为unix有效时间戳的最简单方法是什么?

I was wondering what would be the easiest way to change in laravel to save timestamps in database as unix valid timestamp?

提前谢谢!

推荐答案

请阅读有关Laravel日期更改器的信息: https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

Please read about Laravel date mutators: https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

默认情况下,时间戳记格式为"Y-m-d H:i:s".如果你需要 自定义时间戳格式,在您的网站上设置$ dateFormat属性 模型.此属性确定日期属性如何存储在 数据库,以及模型序列化为 数组或JSON

By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON

此外,您可以覆盖getDateFormat():

protected function getDateFormat()
{
    return 'U';
}

并在您的迁移文件中使用它:

And use this in your migration files:

$table->integer('updated_at');
$table->integer('created_at');

而且,如果您使用软删除:

And, if you use soft deletes:

$table->integer('deleted_at');

https://laravel.com/docs/4.2/eloquent#timestamps

这篇关于Laravel时间戳将时间节省为Unix时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆