久久久久久久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)
主站蜘蛛池模板: 欧美精品一区二区三区蜜桃视频 | 久久机热| 精品国产一区二区国模嫣然 | 狠狠色香婷婷久久亚洲精品 | 天天弄天天操 | 91久久北条麻妃一区二区三区 | 亚洲美女一区二区三区 | 国产一二三区在线 | 亚洲精品第一 | 欧美一区二区三区视频在线播放 | 天堂成人国产精品一区 | 久草网在线视频 | 一级黄色生活视频 | 日韩aⅴ片 | 亚洲国产成人久久综合一区,久久久国产99 | 一级欧美 | 亚洲国产精品激情在线观看 | 精品久久久久久亚洲综合网站 | 久草在线中文888 | 国产在线视频一区 | 久草网站 | 羞视频在线观看 | 中文字幕免费视频 | 欧美午夜精品 | 国产午夜精品视频 | 国产精品久久久久久亚洲调教 | 久久免费视频1 | 欧美日韩一区在线观看 | 精品亚洲一区二区三区四区五区 | 欧美福利 | www国产成人免费观看视频,深夜成人网 | 久久亚洲欧美日韩精品专区 | 在线观看成年视频 | 日韩精品免费视频 | 蜜桃五月天 | 免费看一区二区三区 | 亚洲黄色av网站 | 91在线看 | 亚洲精品久久久久久宅男 | 久久精品国产一区二区电影 | 91porn成人精品 |