Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPlacid Rodrigues
12,630 PointsGetting Fatal Error: Call to a member function format() on a non-object
Hi, I am getting Fatal Error in the browser when I test the following code as shown in lecture:
<?php
$raw = '10 . 11 . 1968';
$date = DateTime::createFromFormat('d . m. Y', $raw);
?>
<p>The output date is: <?php echo $date->format("m/d/Y"); ?> </p>
The Fatal Error is: Fatal error: Call to a member function format() on a non-object in C:\wamp\www\tt\tt_php_best_practices\date_time.php on line 14
Can anyone please tell me how to solve the issue?
Thanks,
Placid
1 Answer
Jose Soto
23,407 PointsYou're just missing a space after the 'm' character. The CreateFromFormat method of the DateTime object will read the format exactly as it is entered. If it can't create a new DateTime object from that string, then it returns a bool false.
This should work:
<?php
$raw = '10 . 11 . 1968';
$date = DateTime::createFromFormat('d . m . Y', $raw);
?>
<p>The output date is: <?php echo $date->format("m/d/Y"); ?> </p>
Placid Rodrigues
12,630 PointsPlacid Rodrigues
12,630 PointsThanks Jose for clear explanation.