問題描述
我需要在 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模板網!