簡體   English   中英

Laravel Carbon 返回不正確的時間

[英]Laravel Carbon returns incorrect time

I have an issue with my Laravel app that I'm developing, the returns time is incorrect, I did specify the timezone on both config/app.php and php.ini file, and I did clear the cache on Laravel using the following commands :

php artisan cache:clear
php artisan view:clear
php artisan config:cache

此外,在php.ini文件中更改時區后,我重新啟動了 PHP 服務。 我正在使用 XAMPP 作為服務器。

請注意:唯一正確的時間是使用純 php 函數!

任何人都可以幫助我解決這個問題嗎?

在此處輸入圖像描述

時間正確:字符串末尾的Z表示GMT (祖魯時區)。

When a Carbon instance is output in a JSON response, it's always converted in the ISO-8601 string and given in GMT timezone, as it's the standard and very recommended way to exchange date information between systems (and so in a JSON API). 如果您嘗試在瀏覽器中傳遞new Date('2020-08-20T06:46:34.407Z') ,您會看到瀏覽器自動將其轉換為用戶時區。

如果你試試:

Carbon::now()->format('Y-m-d H:i:s.u')

您將看到它使用您選擇的時區。 你也讓它出現在 output 中:

Carbon::now()->format('Y-m-d H:i:s.u e')

因此,不同時區的系統/瀏覽器不會搞砸。

最好的辦法仍然是在您的服務器端使用 UTC,並且僅在您想在您知道位於該時區的特定用戶上顯示日期的最后一刻使用 Asia/Riyadh。

我在這里更詳細地解釋了它: https://medium.com/@kylekatarnls/always-use-utc-dates-and-times-8a8200ca3164

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM