PHP Fatal error: Call to a member function format() on boolean
Neither example work as you have multiple errors:
- You forgot your second parameter to
Datetime::createFromFormat()
h:i:s
should beH:i:s
- Your date in the second example is separated by a
.
not a-
Fixes:
<?php
$date = "13-06-2015 23:45:52";
echo DateTime::createFromFormat('d-m-Y H:i:s', $date)->format('Y-m-d h:i:s');
$date = "10.06.2015 09:25:52";
echo DateTime::createFromFormat('d.m.Y H:i:s', $date)->format('Y-m-d h:i:s');
?>
Call to a member function format() on boolean in PHP LARAVEL
http://php.net/manual/en/datetime.createfromformat.php
DateTime::createFromFormat()
returns false on failure. Since the hour goes from <= 12 to 13 in your example, that indicates to me that the "h" should be capitalized.
$date = DateTime::createFromFormat('Y-m-d\TH:i', $request->date_start);
DateTime::createFromFormat(): Call to a member function format() on bool
There is colon between the hour and the minutes, instead of hyphen:
$date = DateTime::createFromFormat("Y-m-d*H:i", $_POST["registrationDeadline"]);
PHP DateTime Issue: Call to a member function format() on boolean
Ok. I've solved the issue. I started var_dump-ing all the variables until I noticed something...
C:\wamp64\www\a\datetime.php:47:string '12/2/17</URD>' (length=13)
While print and echo show only '12/2/17', var_dump shows trailing data "</URD>" which is a tag I have in the database table I'm calling from. I'm not sure why the complete data isn't being shown on print or echo, but a simple str_replace to my $dateMDY variable removed the unseen </URD> and now prints the correct format.
Hopefully this can help someone in the future who may be scratching their heads at unseen data being attached to their variables!
Thanks to @Mehrdad Dastgir for illuminating an idea for me.
PHP Fatal Error Call to a member function format() on boolean
Do it like this, there are few problems in your code.
Problems:
1.
$end_date
is not initialized. just changing this will also not work. check here2.
DateTime::createFromFormat
expects second parameter to be time string (like this15-Feb-2009
)not timestamp
.
Try this code snippet here
ini_set('display_errors', 1);
$unixtimestamp = 1496102399;
$date = new DateTime();
$date->setTimestamp($unixtimestamp);
echo $end_date = $date->format('Y-m-d');
For displaying the date in Deutsch
language you can use this, prerequisites are listed below of this code doesn't work.
<?php
ini_set('display_errors', 1);
$loc=setlocale(LC_ALL,'de_DE');
echo strftime('%d %B %Y',1496102399);
1.
locale -a
Listing all locale's present on your system, make surede_DE
,de_DE.iso88591
,de_DE.utf8
these locale's are present on your system2.
locale-gen de_DE
3.
dpkg-reconfigure locales
reconfigure locales
Call to a member function format() on boolean
add condition to check whether you got the diff or not, as it returns false if there is error . Check manual for the same
$diff = date_diff($indexing_value, $current_value);
if ($diff) {
if ($diff->format("%R%a") < 0) {
echo "1";
}else{
echo "2";
}
}
You are getting error because for some values the diff is not calculated and have value False
in $diff
Shows Call to a member function format() on boolean error in php after editing
In order for php to parse variables within stings properly, you must wrap them in double quote marks (").
Change
$d1 = DateTime::createFromFormat('Y-m-d', '$y-$m-$d');
to
$d1 = DateTime::createFromFormat('Y-m-d', "$y-$m-$d");
Related Topics
How to Add Two Strings Together
Default as First Option in Switch Statement
How Random Is PHP's Shuffle Function
Fatal Error: Call to Undefined Function Openssl_Random_Pseudo_Bytes()
Detect If Cookies Are Enabled in PHP
Using a Variable Outside of the While Loop (Scope)
Illegal Command Error Code 127 in PHP Exec Function
PHP Remember File Field Contents
Extract Floating Point Numbers from a Delimited String in PHP
Opposite of Nl2Br? Is It Str_Replace
Php, Merging Arrays with Common Keys
PHP - Sort Multi-Dimensional Array by Another Array
Why Can't I Access the Array with Index Directly
Using Gcm to Send Notifications on App, Returns Invalidregistration Error
My Laravel 5.2.10 Sessions Wont Persist
How to Make Sure That Values from MySQL Keep Their Type in PHP