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.
Related
I'm having some troubles using the format function in Symfony2 when trying to insert a date interval into a table, for the purpose of setting due dates for created invoices.
Here is what I have:
$today = new \DateTime();
$interval = $today->add(new \DateInterval('P1M'));
$invoice->setDueDate($interval->format('Y-m-d H:i:s'));
However, when I hover over the format parameter in PHPStorm, it tells me that it's expecting a DateTime object not a string, and I get the following error in my profiler:
Error: Call to a member function format() on string
So, I changed the line to this:
$invoice->setDueDate($interval->format(new \DateTime()));
But when I run that, my profiler gives this error:
Warning: DateTime::format() expects parameter 1 to be string, object given
It almost seems like a catch 22 situation! I am really baffled, do I use a string or a DateTime object, because either one fails yet warns me I need to use one or the other..
Any ideas?
I’d guess that $invoice->setDueDate() is the one expecting a DateTime instance. So the line should be $invoice->setDueDate($interval);
I am getting an error with displaying the value:
$thedate = $row2['date'];
echo $thedate;
In php, which is a value from the database ($thedate) is "2015-05-05 21:52:31.000"
How can I format it to be able to display it on the php page as a string? Currently it shows error "Object of class DateTime could not be converted to string".
You have a DateTime object, so you have to use format() to format your output, e.g.
echo $thedate->format("Y-m-d");
I managed to dynamically load and compare several time values in PHP.
Right now I am stuck here:
$additional_time = $entry_start->diff($compare_from_timeformat);
$additional_time ->format("H:i");
$avaliabletime->modify('+1 hours');
I want to replace the +1 with $avaliabletime but if i try something like this:
$avaliabletime->modify('+'.$additional_time.' hours');
I get this error:
Catchable fatal error: Object of class DateInterval could not be converted to string
So I got 2 questions now.
is there a way to use a variable with the modify part ?
can I also add minutes in the same string ? for example $avaliabletime->modify('+01:45 hours'); ?
$additional_time is a DateInterval object, not a DateTime object or string. To modify your DateTime object by the amount that DateInterval represents use DateTime::add():
$additional_time = $entry_start->diff($compare_from_timeformat);
$avaliabletime->add($additional_time);
If you want to add additional time then you can use DateTime::modify():
$additional_time = $entry_start->diff($compare_from_timeformat);
$avaliabletime->add($additional_time);
$avaliabletime->modify('+45 minutes');
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));
Another simple question. I found this really cool snippet of code:
$date_str = "Jan 14th 2011";
$date = date_parse_from_format('M jS Y', $date_str);
echo $date->format('Y-m-d');
But when I run it on my computer it says Fatal error: Call to a member function format() on a non-object line 3.
The code was taken from here Converting date string to date so I presume it is good but it looks like it is referencing to an object that does not exist.
I have been looking at http://php.net/manual/en/function.date-parse-from-format.php and http://www.w3schools.com/php/php_ref_date.asp amongst many others but I have not found any any clues.
My question is should this code work as a standalone piece of code. If so why does it not work for me? Else, should I do to get it to work as expected.
date_parse_from_format();
returns associative array and you are trying to access the class method on a non object.
if you want to make use of PHP's inbuilt DateTime class. then more information here http://in2.php.net/manual/en/datetime.format.php
date_parse_from_format returns an array, not a DateTime object. What you want is
$date = date_create_from_format('M jS Y', $date_str);
^^^^^^---note the change
echo date('Y-m-d', $date);