Php Laravel Carbon会根据时区更改时间,但不会更改日期
当我创建付款时,它将以UTC时间存储在数据库中Php Laravel Carbon会根据时区更改时间,但不会更改日期,php,laravel,Php,Laravel,当我创建付款时,它将以UTC时间存储在数据库中 # id, paymentrequest_id, name, note, currency, amount, created_at, updated_at 3, 1, Test, test, Euro (€), 10.00, 2019-03-29 14:27:15, 2019-03-29 14:27:15 当我加载带有时区(欧洲/阿姆斯特丹)的页面时,我看到了正确的时间(GMT+1) 但其格式应为29-03-2019 15:27:15 当我使用V
# id, paymentrequest_id, name, note, currency, amount, created_at, updated_at
3, 1, Test, test, Euro (€), 10.00, 2019-03-29 14:27:15, 2019-03-29 14:27:15
当我加载带有时区(欧洲/阿姆斯特丹)的页面时,我看到了正确的时间(GMT+1)
但其格式应为29-03-2019 15:27:15
当我使用VPN连接到美国(纽约)时。它向我展示了这个
2019-03-29 10:27:15
这是正确的时间,但日期应该是2019年03月29日
这是我使用的代码
$ip = file_get_contents("http://ipecho.net/plain");
$url = 'http://ip-api.com/json/'.$ip;
$tz = file_get_contents($url);
$tz = json_decode($tz,true)['timezone'];
foreach ($payments as $payment)
{
$payment->created_at = Carbon::parse($payment->created_at)->timezone($tz)->toDateTimeString();
}
我不确定这是否是我第一次使用碳的最好的解决方案
如果有人能给我指出正确的方向,我将不胜感激。除非您另有规定,否则日期格式始终为Y-m-d。@aynber我将如何根据时区进行更改?时区没有任何区别。
toDateTimeString()
的定义只返回return$this->rawFormat('Y-m-dh:i:s')
(请参阅)您可以尝试formatLocalized()
或isoFormat()
,但这取决于区域设置,而不是时区。(请参阅)您是否尝试过$date->setTimezone($tz)
就在$payment->created_at
之前,看起来您正在将动态时区设置为created_date,所以我认为您需要先设置时区
$ip = file_get_contents("http://ipecho.net/plain");
$url = 'http://ip-api.com/json/'.$ip;
$tz = file_get_contents($url);
$tz = json_decode($tz,true)['timezone'];
foreach ($payments as $payment)
{
$payment->created_at = Carbon::parse($payment->created_at)->timezone($tz)->toDateTimeString();
}