Laravel 时间戳显示毫秒

Laravel timestamps to show milliseconds(Laravel 时间戳显示毫秒)
本文介绍了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 时间戳显示毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)