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");
Related
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 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));
i have a date string from my db, the date is 16/11/2010 and its format is d/m/Y, i want to modify its like this.
<?php
$date_from_db= '16/11/2010'; // format is d/m/Y
$date = new DateTime($date_from_db);
$date-> modify('+1 week');
echo $date-> format('d/m/Y') ;
?>
i have got this error
Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (16/11/2012) at position 0 (1).
How can i fix this?
try
$date = DateTime::CreateFromFormat("d-m-y", "16-11-2010");
Unfortunately that is not one of the time formats supported by DateTime class constructor.
This page shows valid formats
http://www.php.net/manual/en/datetime.formats.date.php
You should use an actual SQL-compliant date field (typically YYYY-MM-DD) in your DB. Or if you can't change the way your are storing the dates, use DateTime::CreateFromFormat as suggested in other answer.
I would highly suggest using a more standard storage format though.
I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL