Blaze Tama Blaze Tama - 1 year ago 145
PHP Question

"Call to a member function format() on a non-object" when converting date in PHP

First, im a beginner in PHP so please bear with me.

I think converting date is a very basic operation, and shouldnt be that hard. But anyway, i still cant get rid of this error message :

Call to a member function format() on a non-object

So, i go on googling and get some good source like this question.

I tried to do something similiar like that question, but i failed. This is my code :

$temp = new DateTime();
/*ERROR HERE*/ $data_umat['tanggal_lahir'] = $data_umat['tanggal_lahir']->format('Y-m-d');
$data_umat['tanggal_lahir'] = $temp;

So, i did trial & errors, and i found out if i do this :

$data_umat['tanggal_lahir'] = date("Y-m-d H:i:s");

The date will successfully converted, BUT it always return today's date (which i dont want).

All i want is just to convert the date so 10/22/2013 will be 2013-10-22.

Any help is appreciated, Thanks for your time.

Answer Source

You are calling method format() on non-object. Try this:

$data_umat['tanggal_lahir'] = new DateTime('10/22/2013');
$data_umat['tanggal_lahir'] = $data_umat['tanggal_lahir']->format('Y-m-d');

or one-liner:

$data_umat['tanggal_lahir'] = date_create('10/22/2013')->format('Y-m-d');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download