7

I can not 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 StackOverflow question.

I tried to do something similar, 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).

I want to convert the date so that 10/22/2013 will be 2013-10-22.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129

7 Answers7

9

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');
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • +1 for the 1 liner. I prefer to use the `Y-m-d` format though. – bansi Oct 07 '13 at 08:56
  • 2
    @bansi: This one-liner is kinda funny to me, because first part (`date_create`) is procedural style, and second one (`->format()`) is object oriented style. Perfect example of object oriented style one-liner would be `(new DateTime('10/22/2013'))->format('Y-m-d')`, but that is for PHP >= 5.4.0, since the example in answer is for PHP >= 5.2.0. – Glavić Oct 07 '13 at 09:03
  • @bansi: OP said he has input format `10/22/2013` that he needs to change to `Y-m-d`. `10/22/2013` is valid/supported [input format](http://www.php.net/manual/en/datetime.formats.php) of date (american format = m/d/Y). – Glavić Oct 07 '13 at 09:08
3

You can use strtotime() to convert this. Its converts the given date to a timestamp and then by using date() function you can convert the timestamp to desired date format.

Try this..

$date = '10/22/2013';
$timestamp = strtotime($date);
$new_date = date('Y-m-d',$timestamp );
dashbh
  • 682
  • 7
  • 20
1

$data_umat['tanggal_lahir'] is not an instance of DateTime, however $temp is.

Is $data_umat['tanggal_lahir'] meant to be be an instance of DateTime

bear
  • 11,364
  • 26
  • 77
  • 129
1

$data_umat['tanggal_lahir'] is not an instanceof of object DateTime

use to make it instance of DateTime

$data_umat['tanggal_lahir'] = new DateTime();
Shushant
  • 1,625
  • 1
  • 13
  • 23
1

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

mak-hack
  • 11
  • 1
1

when using Symfony or Slim with Doctrine, try this:

//form[birthday = "2000-12-08"]

[...]

$birthday = \DateTime::createFromFormat("Y-m-d",$formData['birthday']);

$obejct = new $object();
$object->setBirthday($birthday);

[...]

-2

If you encounter this issue when using Symfony, try this hack:

Open:Your Symfony Root/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php

Go to line 50 something where the function convertToDatabaseValue() is declared.

The original code:

return ($value !== null)
        ? $value->format($platform->getDateTimeFormatString()) : null;

Change to:

 return ($value !== null)
            ? $value : null;

Seems Symfony is doing an extra conversion when passing a string as the datestring.

Directly passing a DateTime ojbect won't work as it will prompt another error saying "Object DateTime can't be converted to string".

TaylorR
  • 3,746
  • 5
  • 25
  • 42