php 在数据库中找不到 DateTime 时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34861317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DateTime timezone not found in database
提问by Uffo
Did anyone else had this strange problem? Error message:
有没有其他人遇到过这个奇怪的问题?错误信息:
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (01/18/2016 00:00 AM America/New_York) at position 17 (A): The timezone could not be found in the database'
Exception: DateTime::__construct(): Failed to parse time string (01/18/2016 00:00 AM America/New_York) at position 17 (A): The timezone could not be found in the database
致命错误:未捕获的异常 'Exception' 带有消息 'DateTime::__construct(): 无法解析时间字符串 (01/18/2016 00:00 AM America/New_York) 在位置 17 (A):找不到时区在数据库'
异常:DateTime::__construct(): 无法解析时间字符串 (01/18/2016 00:00 AM America/New_York) 在位置 17 (A):在数据库中找不到时区
Original PHP Code:
原始 PHP 代码:
$datetime = new DateTime(trim(html_entity_decode($this->input->post('publish_date').' '.$_POST['schedule_time'].' '.$_POST['schedule_meridian'] . ' ' .$_POST['schedule_timezone'])));
$date = $datetime->format('D, d M Y H:i:s O');
回答by felipsmartins
I afraid you've created DateTime object like this:
恐怕您已经像这样创建了 DateTime 对象:
$date = new DateTime('01/18/2016 00:00 AM America/New_York');
That is not a supported/valid datetime format!
这不是受支持的/有效日期时间格式!
If you want to create a DateTime object from another format you must call DateTime::createFromFormat()instead, look:
如果要从另一种格式创建 DateTime 对象,则必须改为调用DateTime::createFromFormat(),请查看:
$timezone = new DateTimeZone('America/New_York');
$strdate = '01/18/2016 00:00 AM';
$date = DateTime::createFromFormat('m/d/Y H:i A', $strdate, $timezone);
PHP doc states:
PHP 文档指出:
DateTime::createFromFormat / date_create_from_format — Returns new DateTimeobject formatted according to the specified format
DateTime::createFromFormat / date_create_from_format — 返回根据指定格式格式化的新 DateTime对象