久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 時間戳顯示毫秒的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: 日韩中文字幕 | av基地网 | jlzzjlzz欧美大全| 狠狠干| 久久视频一区 | 日本看片网站 | 午夜在线观看视频网站 | 亚洲乱码在线观看 | 五月天婷婷激情 | 久久久综合 | 亚州av在线| 人人插人人射 | 欧美视频三区 | 日韩一区二区av | 欧美日韩大片 | 中文字幕免费看 | 国产精品美女 | 亚洲国产精品久久 | 午夜国产 | 人人爱人人草 | 青草av在线 | 亚洲不卡视频 | 一级片观看 | 97cao| 午夜在线视频 | 国产成人在线播放 | 国产精品成人在线 | 黄色一级大片在线免费看产 | 国产手机在线视频 | 亚洲午夜激情 | 国产精品久久久久久久久久 | 国产精品99久久久久久www | 亚洲精品免费视频 | 日韩高清在线播放 | 久久免费视频观看 | 午夜久久久 | 国产成人免费在线视频 | 黄色影院在线观看 | 欧美日韩一区二区三区视频 | 五月婷婷网 | 成人国产在线 |