Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

php - DateTime timezone not found in database

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

Original PHP Code:

$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');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I afraid you've created DateTime object like this:

$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:

$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:

DateTime::createFromFormat / date_create_from_format — Returns new DateTime object formatted according to the specified format


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...