Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string

Asked
Active3 hr before
Viewed126 times

6 Answers

faileduncaughterror
90%

( ! ) Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (06-28-2014 07:43:58 ) at position 0 (0): Unexpected character' in /Users/matt/Desktop/Likes/forgot/activate.php on line 17, Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português

The date string you're passing is not supported by the DateTime parser. You must create a DateTime object by using createFromFormat. This method allows you to specify the custom format when creating a new DateTime object:

$my_dt = DateTime::createFromFormat('m-d-Y H:i:s', $token_created_at);

If you're still getting an error that means that your $token_created_at is not in the format you specified:

$now = date('m-d-Y H:i:s'); //string(19) "06-28-2014 15:00:47"

var_dump(DateTime::createFromFormat('m-d-Y H:i:s', $now));
object(DateTime) #1 (3) {
  ["date"]= >
   string(19)
"2014-06-28 15:00:47" ["timezone_type"] =>
int(3)["timezone"] =>
   string(13)
"Europe/Berlin"
}

I see your problem - the format string has a space after s. The format strings must match exactly:

$my_dt = DateTime::createFromFormat('m-d-Y H:i:s ', $token_created_at);
load more v
88%

About WordPressAbout WordPressWordPress.orgDocumentationSupportFeedback

Code Which I am using :

< ? php
eventDate = new DateTime(get_field('event_date'));
echo $eventDate - > format('M') ?
   >
load more v
72%

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (03-22-2016) at position 0 (0): Unexpected character' in /home/budweiser/public_html/verify.php:13 Stack trace: #0 /home/budweiser/public_html/verify.php(13): DateTime->__construct('03-22-2016') #1 /home/budweiser/public_html/verify.php(35): age('10-10-1982') #2 {main} thrown in /home/budweiser/public_html/verify.php on line 13,Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (03-22-2016) at position 0 (0): Unexpected character' in /home/budweiser/public_html/verify.php:13 Stack trace: #0 /home/budweiser/public_html/verify.php(13): DateTime->__construct('03-22-2016') #1 /home/budweiser/public_html/verify.php(35): age('03-10-1991') #2 {main} thrown in /home/budweiser/public_html/verify.php on line 13,Lost the first line of the function, there’s no need to do that extra process on the $dob variable. Just pass the string you started with into the Datetime construction.,Fatal error: Uncaught exception ‘Exception’ with message ‘DateTime::__construct(): Failed to parse time string (1970197019701970-JanJan-ThuThu) at position 7 (0): Double date specification’ in /home/budweiser/public_html/verify.php:11 Stack trace: #0 /home/budweiser/public_html/verify.php(11): DateTime->__construct(‘197019701970197…’) #1 /home/budweiser/public_html/verify.php(35): age(’–’) #2 {main} thrown in /home/budweiser/public_html/verify.php on line 11

Shouldn’t this

    $date2 = new DateTime(date("m-d-Y"));

be just

    $date2 = new DateTime("now");
load more v
65%

The correct way to pass a UNIX timestamp to DateTime::__construct() is to prepend @ to the timestamp:, Why GitHub? Features → Mobile → Actions → Codespaces → Packages → Security → Code review → Issues → Integrations → GitHub Sponsors → Customer stories → , Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. , Pricing Plans → Compare plans → Contact Sales → Education →

Exception
DateTime::__construct(): Failed to parse time string(1518181555) at position 7(5): Unexpected character
1. in /app/vendor / 2 amigos / yii2 - usuario / src / User / Model / User.php at line 347
/**
 * Returns password age in days
 * @return integer 
 */
public
function getPassword_age() {
   if (is_null($this - > password_changed_at)) {
      return $this - > getModule() - > maxPasswordAge;
   }
   $d = new\ DateTime($this - > password_changed_at);
   return $d - > diff(new\ DateTime(), true) - > format("%a");
}
}
2. in /app/vendor / 2 amigos / yii2 - usuario / src / User / Model / User.php at line 347DateTime::__construct('1518181555') *
   /
public
function getPassword_age() {
   if (is_null($this - > password_changed_at)) {
      return $this - > getModule() - > maxPasswordAge;
   }
   $d = new\ DateTime($this - > password_changed_at);
   return $d - > diff(new\ DateTime(), true) - > format("%a");
}
}
load more v
75%

I had this construction error when trying to creating a new DateTime object using a timestamp:,Exception: DateTime::_construct(): Failed to parse time string (1372622987) at position 8 (8): Unexpected character in DateTime->_construct(),The reason this is very strange: I since ran a script to create a new DateTime object with the timestamp passed in as a hard coded value, and it reported no errors.,The date string you're passing is not supported by the DateTime parser. You must create a DateTime object by using createFromFormat. This method allows you to specify the custom format when creating a new DateTime object:

The object creation code is:

$start_date = new DateTime("@{$dbResult->db_timestamp}");
load more v
40%

Be nice, respectful and patient. Inflammatory or inappropriate posts will get your post nuked and flood your life with bans and bad karma.,This question has been answered by multiple community members. See the first response.,MODX Cloud makes presenting to clients a breeze. I can decide which website version to show at any time.

Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string(CST6CDT) at position 3(6): Unexpected character in /home/username / public_html / manager / controllers /
   default / dashboard / widget.grid - online.php: 17 Stack trace: #0 /home/username/public_html/manager/controllers/default/dashboard/widget.grid-online.php(17): DateTime->__construct('CST6CDT') # 1 / home / username / public_html / core / model / modx / moddashboardwidget.class.php(246): modDashboardWidgetWhoIsOnline - > render() #2 /home/username/public_html/core/model/modx/moddashboardwidget.class.php(118): modDashboardWidgetInterface->process() # 3 / home / username / public_html / core / model / modx / moddashboard.class.php(78): modDashboardWidget - > getContent(Object(WelcomeManagerController)) #4 /home/username/public_html/manager/controllers/default/welcome.class.php(64): modDashboard->render(Object(WelcomeManagerController)) # 5 / home / username / public_html / core / model / modx / modmanagercontroller.class.php(149): WelcomeManagerController - > process(Array) #6 /home/username/public_ in /home/username/public_html/manager/controllers/default/dashboard/widget.grid-online.php on line 17
load more v

Other "failed-uncaught" queries related to "Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string"