Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...