数据库中 UTC 中有timestamp
字段(非空),其值为: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
的某个地方是空的?