954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

date_format ()

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

ebanbury
Junior Poster
158 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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

ebanbury
Junior Poster
158 posts since Aug 2010
Reputation Points: 10
Solved Threads: 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.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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
urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

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

cheers liz

ebanbury
Junior Poster
158 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

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

echo date('Y-m-d', $row_rs_propdetails['add_date']);
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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'];
Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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

ebanbury
Junior Poster
158 posts since Aug 2010
Reputation Points: 10
Solved Threads: 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!!

ebanbury
Junior Poster
158 posts since Aug 2010
Reputation Points: 10
Solved Threads: 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

urtrivedi
Nearly a Posting Virtuoso
1,306 posts since Dec 2008
Reputation Points: 257
Solved Threads: 270
 

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' );
mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: