PHP中的日期函数 [英] Date functions in PHP

查看:82
本文介绍了PHP中的日期函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个简单的问题。我发现这个很棒的代码片段:

  $ date_str =2011年1月14日; 
$ date = date_parse_from_format('M jS Y',$ date_str);
echo $ date-> format('Y-m-d');

但是当我在我的电脑上运行它时,会说致命错误:调用成员函数格式()在非对象行上3。



代码从这里取得将日期字符串转换为日期,所以我认为它是好的,但它看起来像是引用不存在的对象。



我一直在看 http://php.net/ manual / en / function.date-parse-from-format.php http:/ /www.w3schools.com/php/php_ref_date.asp ,但我没有发现任何线索。



我的问题是这个代码是否正常工作作为独立的代码片段。如果是这样,为什么它不适合我?否则,我应该如何使其按预期工作。

解决方案

  date_parse_from_format() ; 

返回关联数组,并尝试访问非对象上的类方法。



如果要使用PHP内置的DateTime类。那么这里有更多的信息 http://in2.php.net/manual/en/datetime .format.php


Another simple question. I found this really cool snippet of code:

$date_str = "Jan 14th 2011";
$date = date_parse_from_format('M jS Y', $date_str);
echo $date->format('Y-m-d');

But when I run it on my computer it says Fatal error: Call to a member function format() on a non-object line 3.

The code was taken from here Converting date string to date so I presume it is good but it looks like it is referencing to an object that does not exist.

I have been looking at http://php.net/manual/en/function.date-parse-from-format.php and http://www.w3schools.com/php/php_ref_date.asp amongst many others but I have not found any any clues.

My question is should this code work as a standalone piece of code. If so why does it not work for me? Else, should I do to get it to work as expected.

解决方案

date_parse_from_format();

returns associative array and you are trying to access the class method on a non object.

if you want to make use of PHP's inbuilt DateTime class. then more information here http://in2.php.net/manual/en/datetime.format.php

这篇关于PHP中的日期函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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