发现意外数据日期格式?
Unexpected data found date format?
数据库中有 timestamp
个 UTC 字段(非空),值为:2019-08-09 20:06:08
。
我尝试使用函数将这个时间转换为本地时间:
private function toLocalTime($dateInUTC) {
$time = strtotime($dateInUTC.' UTC');
return date("d/m/Y H:i", $time);
}
转换为:
$response = []; // Data is present here
foreach($response as $v) {
$v->created_at = $this->toLocalTime($v->created_at);
}
在我调用 $this->toLocalTime()
的线路上出现异常:
我试过这个代码:
$timestamp = '2019-08-09 10:41:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
$date->setTimezone('your/timezone'); //
$new_date = $date->format('your new format'); // Y-m-d H:i:s
也许我的 $timestamp
某处是空的?
据我所知,您的项目中有 Carbon:
$timestamp = '2019-08-09 10:41:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
$date->setTimezone('America/Argentina/Buenos_Aires'); // or your timezone
$new_date = $date->format('Y-m-d H:i:s'); // or your new format
在你的例子中:
private function toLocalTime($dateInUTC)
{
// parse your date with carbon and set that as UTC
$date = Carbon::createFromFormat('Y-m-d H:i:s', $dateInUTC, 'UTC');
// set your timezone to get a local time.
$date->setTimezone('America/Argentina/Buenos_Aires'); // or your timezone
return = $date->format('Y-m-d H:i:s'); // or your new format
}
https://www.php.net/manual/en/timezones.php
如果您没有安装 Carbon 作为示例使用本机 php:
private function toLocalTime($dateInUTC)
{
$utc_date = \DateTime::createFromFormat('Y-m-d H:i:s', $dateInUTC), new DateTimeZone('UTC'));
$utc_date->setTimeZone(new \DateTimeZone('America/Argentina/Buenos_Aires'); // or your timezone
return $utc_date->format('Y-m-d H:i:s'); // or your new format
}
数据库中有 timestamp
个 UTC 字段(非空),值为:2019-08-09 20:06:08
。
我尝试使用函数将这个时间转换为本地时间:
private function toLocalTime($dateInUTC) {
$time = strtotime($dateInUTC.' UTC');
return date("d/m/Y H:i", $time);
}
转换为:
$response = []; // Data is present here
foreach($response as $v) {
$v->created_at = $this->toLocalTime($v->created_at);
}
在我调用 $this->toLocalTime()
的线路上出现异常:
我试过这个代码:
$timestamp = '2019-08-09 10:41:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
$date->setTimezone('your/timezone'); //
$new_date = $date->format('your new format'); // Y-m-d H:i:s
也许我的 $timestamp
某处是空的?
据我所知,您的项目中有 Carbon:
$timestamp = '2019-08-09 10:41:00';
$date = Carbon::createFromFormat('Y-m-d H:i:s', $timestamp, 'UTC');
$date->setTimezone('America/Argentina/Buenos_Aires'); // or your timezone
$new_date = $date->format('Y-m-d H:i:s'); // or your new format
在你的例子中:
private function toLocalTime($dateInUTC)
{
// parse your date with carbon and set that as UTC
$date = Carbon::createFromFormat('Y-m-d H:i:s', $dateInUTC, 'UTC');
// set your timezone to get a local time.
$date->setTimezone('America/Argentina/Buenos_Aires'); // or your timezone
return = $date->format('Y-m-d H:i:s'); // or your new format
}
https://www.php.net/manual/en/timezones.php
如果您没有安装 Carbon 作为示例使用本机 php:
private function toLocalTime($dateInUTC)
{
$utc_date = \DateTime::createFromFormat('Y-m-d H:i:s', $dateInUTC), new DateTimeZone('UTC'));
$utc_date->setTimeZone(new \DateTimeZone('America/Argentina/Buenos_Aires'); // or your timezone
return $utc_date->format('Y-m-d H:i:s'); // or your new format
}