Laravel 时间戳显示毫秒

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

问题描述

我需要在 Laravel 应用程序上以高精度存储 updated_at 时间戳,使用格式 "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 的架构构建器在数据库中添加了一个类型为时间戳的列,并且根据 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 中,在 Schema 回调之外做:

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 个小数位,但是如果你愿意,你最多可以有 6 个,通过在改变表和 sprintf 中的两个地方将 3 更改为 6,并将乘数 * 1000 调整为 10000006.

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 年了,没有任何改变,所以我不抱希望.如果您对详细信息感兴趣,请参阅此错误报告:http://grokbase.com/t/php/php-bugs/11524dvh68/php-bug-bug-54648-new-pdo-forces-format-of-datetime-fields我在另一个答案中找到了这个链接,它可能会帮助您更了解这个问题: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 时间戳显示毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:Laravel 5.2:Auth::logout() 不起作用 下一条:Laravel 5 mutator 仅在我创建记录时有效,而在我更新记录时无效

相关文章

最新文章