I simply just want to convert the date in PHP. I am using PHP 7.0.33. It was working fine before but suddenly it started producing a Fatal error
Below is my code.
When I use format() I get
Fatal error: Uncaught Error: Call to a member function format() on boolean
$date = DateTime::createFromFormat('d/m/Y', $dates[$i]);
$date = $date->format("Y-m-d");
Any help will be great!!
DateTime::createFromFormat() returns FALSE in case of error, so the problem is in this function call. Check the value of $dates[$i].
Related
I got that script on play ground that works perfect
PlayGround.
in php I got error that from my understanding its the conversation of the date to string
what can be the reason?
can i use other method instead that will work?
I'm storing date as Y-m-d H:i:s in mongodb.
the error is when subtracting $answerTime - $startTime
the error
Fatal error: Uncaught MongoDB\Driver\Exception\CommandException: Error parsing date string ''; 0: Empty string ' in...
I can see that there are many questions along these lines but I am very confused as to why the following does not work, taken straight from the PHP docs:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate;
The error:
PHP Catchable fatal error: Object of class DateTime could not be converted to string.
In fact every example in the docs gives me this error. Any ideas?
You can't echo the DateTime object directly. You have to use the format method to get the date and / or time part:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate->format('Y-m-d H:i:s');
// NOT echo $tempDate!!!
demo: http://ideone.com/IyqRWj
If you want to see the details of the object (for debug) you can use var_dump:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
var_dump($tempDate);
The error message:
PHP Catchable fatal error: Object of class DateTime could not be converted to string.
is self-explanatory. The statement:
echo $tempDate;
attempts to print a DateTime object. The echo() language construct expects a string, you pass it a DateTime object. The DateTime class does not implement the __toString() magic method and PHP doesn't know how to convert a DateTime object to string.
There are so many ways to represent a DateTime object as string and all of them are handled by the DateTime::format() method.
In fact every example in the docs gives me this error.
In fact, every example in the documentation of DateTime::createFromFormat() reads:
echo $date->format('Y-m-d');
which is a different thing that echo $date;.
Any ideas?
Read the documentation of DateTime and DateTime::format() carefully.
I have been using the following code for a couple of months and it's been working fine, but now seems to have an issue with a certain time or time range.
I have a GPS tracker that sends it's date/time like this:
150102235335
The format is ymdhis
$input_array[6] = 150102235335;
$datetime = DateTime::createFromFormat('ymdhis', $input_array[6]);
$datetime = $datetime->format('Y-m-d h:i:s');
Using that time, php crashes with the following error:
PHP Fatal error: Call to a member function format() on a non-object
It seems that datetime ends up empty.
However, using the time 150103004933 works just fine.
Can anyone see where I have gone wrong here, or is this a bug?
Is there a better way to accomplish my date conversion?
I am using PHP 5.4.35
h is 12-hour format; and therefore 23 hours is invalid. Only values in the range 00-11 would be valid.
H is 24-hour format
I am trying to format 2014-03-27 00:53:31 to be: 03/27/2014 I have tried many solutions but none of which have worked. My most recent is explained in this question.
So the time is stored in database as a datetime like: 2014-03-27 00:53:31
I call this by $customer->last_login;
Then I am trying to format this by doing the following:
$dt = $customer->last_login;
echo $dt->format('m/d/Y');
When I run this I get the following error:
Fatal error: Call to a member function format() on a non-object
What am I doing wrong? Or what is a better solution to formatting this to display just the date?
If $customer->last_login is a string, you have to convert to a DateTime object first.
$dt = new DateTime($customer->last_login);
echo $dt->format('m/d/Y');
Try this
echo date('m/d/Y',strtotime($dt));
I suddenly get the error message Fatal error: Call to a member function setTimezone() on a non-object after adding some new lines of code:
first of all, i used DateTime to switch between different timeformats to calculate DST, etc.
I used the following code [Code Block 1]
foreach ($result as $k=>$v)
{$timestamp=DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $k,new DateTimeZone("UTC")); //take current date from $k as new DateTile Object
$timestamp->setTimezone(new DateTimeZone('Europe/Berlin')); //convert timezone
$date[]=$timestamp->format('Y-m-d H:i:s'); // "export" the date to date-array
}
This code works well, until now:
I added 20 code lines above an other DateTime function [Code Block 2]:
$ts=new DateTime("#".round($_GET['startdate']/1000));
$startdate=$ts->format('Y-m-d\TH:i:s.u\Z');
After I added these 2 lines, all the time i get the error: Fatal error: Call to a member function setTimezone()for this line:$timestamp->setTimezone(new DateTimeZone('Europe/Berlin'));
...but i can't see an relation / connection between these to code parts?!
What's happened here?
Thank you, for any idea.
//Update: as you can see at the comments, i'm using PHP 5.3.6
again: the code block 1 works, if i remove the code block 2
Looks like not all of array keys of $result is valid date strings. The DateTime class returns a new DateTime instance or FALSE on failure. And when $k is not valid, $timestamp is false, which is not an object.