Laravel时间戳显示毫秒 [英] Laravel timestamps to show milliseconds

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

问题描述

我需要使用格式"m-d-Y H:i:s.u" (包括毫秒)

I need to store updated_at timestamp with high precision on a laravel application, using the format "m-d-Y H:i:s.u" (including milisseconds)

根据laravel文档,我可以通过在类上设置$ dateFormat属性来自定义日期格式,但是...

According to laravel documentation, I can customize the date format by setting the $dateFormat property on a class, but...

主要问题是,当我使用 $ table-> nullableTimestamps()时,Laravel的架构生成器在数据库中添加了timestamp类型的列,并且根据mysql文档,TIMESTAMP类型的列仅允许精确度可达秒..

The main problem is that Laravel's schema builder adds a column of type timestamp in the database when I use $table->nullableTimestamps() And according to mysql documentation, columns of type TIMESTAMP only allow the precision up to seconds..

关于如何实现这一目标的任何想法?

Any ideas on how I could achieve that?

推荐答案

您不能这样做,因为PHP PDO驱动程序不支持时间戳的小数秒.解决方法是选择时间戳作为字符串,这样PDO驱动程序就不会真正知道它的时间戳,只需执行$query->selectRaw(DB::raw("CONCAT(my_date_column) as my_date_column"))即可,但是这意味着您不能对所有字段都使用默认选择,因此查询成为真正的时间戳.痛.另外,您还需要在模型上覆盖getDateFormat.

You can't because the PHP PDO driver doesn't support fractional seconds in timestamps. A work around is to select the timestamp as a string instead so the PDO driver doesn't know its really a timestamp, simply by doing $query->selectRaw(DB::raw("CONCAT(my_date_column) as my_date_column")) however this means you can't use the default select for all fields so querying becomes a real pain. Also you need to override getDateFormat on the model.

// override to include micro seconds when dates are put into mysql.
protected function getDateFormat()
{
    return 'Y-m-d H:i:s.u';
}

最后,在迁移中,而不是在模式回调之外执行nullableTimestamps:

Finally in your migration rather than nullableTimestamps, outside of the Schema callback do:

DB::statement("ALTER TABLE `$tableName` ADD COLUMN created_at TIMESTAMP(3) NULL");

请注意,此示例用于3个小数位,但是如果您愿意,可以通过在alter table和sprintf中的两个位置将3更改为6,并调整乘数* 1000到1000000,最多可以有6个6.

Note this example was for 3 decimal places however you can have up to 6 if you like, by changing the 3 to a 6 in two places, in the alter table and in the sprintf and also adjusting the multiplier * 1000 to 1000000 for 6.

希望有一天PHP PDO将被更新以解决此问题,但是它已经超过5年了,什么都没有改变,所以我没有希望.如果您对这些细节感兴趣,请参阅以下错误报告: https://stackoverflow.com/a/22990991/259521

Hopefully some day PHP PDO will be updated to fix this, but its been over 5 years and nothings changed so I don't have my hopes up. In case you are interested in the details, see this bug report: http://grokbase.com/t/php/php-bugs/11524dvh68/php-bug-bug-54648-new-pdo-forces-format-of-datetime-fields I found that link in this other answer which might help you more understand the issue: https://stackoverflow.com/a/22990991/259521

最近,PHP确实显示了它的时代,我认为这个问题是我考虑转向更现代的Node.js的原因之一.

PHP is really showing its age lately, and I would consider this issue one of my reasons for considering moving to the more modern Node.js.

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

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