1.11M Members

date_format ()

 
0
 

Hi

Can anyone see what is wrong with this:

<?php echo date_format($row_rs_propdetails['add_date'],'Y-m-d'); ?>

The error message I get is: Warning: date_format() expects parameter 1 to be DateTime,

Any help would be much appreciated.

Basically wanting to bring back the date in add_date column, but just the y-m-d with no time.

Many thanks

 
0
 

Apparently your add_date column is not a datetime column in the database. Read more in the manual.

 
0
 

Yes that is exactly what I've read, but I find it confusing. Apologies. I'll work it out eventually. Thanks

 
0
 

If your column is a varchar, then you can use the date_create before the date_format, as in the first example in the manual.

 
0
 

I think this could help you.

$row_rs_propdetails['add_date']=strtotime($row_rs_propdetails['add_date']);//this will convert mysql text date to php date object
echo  date('Y-m-d',$row_rs_propdetails['add_date'] );//now show date object in required format
 
0
 

Hi THanks for getting back to me. I'll try this :-)
My table colunm is date type.

cheers liz

 
0
 

If your column is a date type then this should work:

echo date('Y-m-d', $row_rs_propdetails['add_date']);
 
0
 

Hi THanks for getting back to me. I'll try this :-)
My table colunm is date type.

cheers liz

if your column is date type then you can simply write as

echo $row_rs_propdetails['add_date'];
 
0
 

Hi

Many thanks for all the input.

My column is a datetime column (as I use the minutes/seconds etc) in another part of the site.

date('Y-m-d', $row_rs_propdetails['add_date']);echo date('Y-m-d', $row_rs_propdetails['add_date']);

This worked - as in it removed the time and just left the date - however it switched all the dates to 1970-01-01

Not sure why as it should just be formatting.....

many thanks

 
0
 

Hi

This has worked perfectly though!!

$row_rs_propdetails['add_date']=strtotime($row_rs_propdetails['add_date']); echo date('Y-m-d',$row_rs_propdetails['add_date'] );//

Many thanks for all the help!!

Question Answered as of 2 Years Ago by pritaeas, urtrivedi and karthik_ppts
 
0
 

This worked - as in it removed the time and just left the date - however it switched all the dates to 1970-01-01

Not sure why as it should just be formatting.....

It will not work because php date and mysql date do not recongize each other.
So when you load php variable with mysql datevalue, its just string for it not date.
So we first convert that string to php date object using strtotime function.
then we use that php date object using various php display formats

 
0
 

date_format() was throwing an error because the first parameter being supplied was not an instance of a PHP DateTime object. It has nothing to do with what the database column is set as.

If you're receiving a 1970-01-01 that means strtotime received a null value, or a value outside of the 1970 - 2038 range approximately. The php DateTime class should be used when possible, to avoid this.

If your date is stored in the database as a datetime field type, this is an easy transition.

<?php
echo DateTime::createFromFormat( 'Y-m-d H:i:s', $row_rs_propdetails['add_date'] )->format( 'Y-m-d' );
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: