We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,018 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

5
Contributors
11
Replies
4 Weeks
Discussion Span
1 Year Ago
Last Updated
12
Views
Question
Answered
ebanbury
Junior Poster
175 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,286 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

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

ebanbury
Junior Poster
175 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 Prodigy
Moderator
9,286 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

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
Posting Virtuoso
1,714 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

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

cheers liz

ebanbury
Junior Poster
175 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

echo date('Y-m-d', $row_rs_propdetails['add_date']);
pritaeas
Posting Prodigy
Moderator
9,286 posts since Jul 2006
Reputation Points: 1,173
Solved Threads: 1,457
Skill Endorsements: 86

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
571 posts since Feb 2011
Reputation Points: 96
Solved Threads: 101
Skill Endorsements: 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

ebanbury
Junior Poster
175 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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
175 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by pritaeas, urtrivedi and Karthik_pranas

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
Posting Virtuoso
1,714 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

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
673 posts since Jul 2008
Reputation Points: 281
Solved Threads: 133
Skill Endorsements: 5

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0910 seconds using 2.68MB