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

Recommended Answers

All 11 Replies

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

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.

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

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

cheers liz

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

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

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'];

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

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!!

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

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' );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.