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

Categories

0 votes
928 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...