“对非对象”上的成员函数格式()的调用“在PHP转换日期时 [英] "Call to a member function format() on a non-object" when converting date in PHP

查看:108
本文介绍了“对非对象”上的成员函数格式()的调用“在PHP转换日期时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是PHP的初学者,请耐心等待。



我认为转换日期是一个非常基本的操作,不应该那么难。但是无论如何,我仍然无法摆脱这个错误信息:



调用非对象上的成员函数格式() c $ c>



所以,我去谷歌搜索,并得到一些好的来源,如这个问题。



我试图做类似这样的事情,但是我失败了。这是我的代码:

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

所以,我做了试用&错误,我发现如果我这样做:

  $ data_umat ['tanggal_lahir'] = date(Ymd H:i :s); 

日期将成功转换,但它总是返回今天的日期(我不想要) p>

我只想转换日期,所以2010年10月22日将是2013-10-22。



解决方案

你正在调用方法 format() / code>非对象。尝试这样:

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

或单班:

  $ data_umat ['tanggal_lahir'] = date_create('10 / 22/2013') - >格式('Ym-d'); 


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.

解决方案

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

这篇关于“对非对象”上的成员函数格式()的调用“在PHP转换日期时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆