在 PHP 中转换日期时“调用非对象上的成员函数 format()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19220143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:02:28  来源:igfitidea点击:

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

phpdate

提问by Blaze Tama

I can not get rid of this error message:

我无法摆脱此错误消息:

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

在非对象上调用成员函数 format()

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

所以,我继续使用谷歌搜索并获得一些像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/2013will be 2013-10-22.

我想将日期转换10/22/20132013-10-22.

回答by Glavi?

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

您正在调用format()非对象的方法。尝试这个:

$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');

回答by dashbh

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.

您可以使用 strtotime() 来转换它。它将给定的日期转换为时间戳,然后通过使用 date() 函数,您可以将时间戳转换为所需的日期格式。

Try this..

尝试这个..

$date = '10/22/2013';
$timestamp = strtotime($date);
$new_date = date('Y-m-d',$timestamp );

回答by Drausio Lucas

when using Symfony or Slim with Doctrine, try this:

在 Doctrine 中使用 Symfony 或 Slim 时,试试这个:

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

//形式[生日=“2000-12-08”]

[...]

[...]

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

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

[...]

[...]

回答by bear

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

$data_umat['tanggal_lahir']不是 的一个实例DateTime,但是$temp是。

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

$data_umat['tanggal_lahir']意味着要成为一个实例DateTime

回答by Shushant

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

$data_umat['tanggal_lahir']不是对象的实例 DateTime

use to make it instance of DateTime

用于使其成为实例 DateTime

$data_umat['tanggal_lahir'] = new DateTime();

回答by mak-hack

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

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

回答by TaylorR

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

如果你在使用 Symfony 时遇到这个问题,试试这个 hack:

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

打开:你的 Symfony Root/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php

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

转到第 50 行convertToDatabaseValue()声明函数的地方。

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.

似乎 Symfony 在将字符串作为日期字符串传递时进行了额外的转换。

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

直接传递 DateTime ojbect 将不起作用,因为它会提示另一个错误,提示“对象 DateTime 无法转换为字符串”。