php 致命错误:调用未定义的方法 DateTime::createfromformat()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8749688/
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
Fatal error: Call to undefined method DateTime::createfromformat()
提问by methuselah
How do you solve the error of:
您如何解决以下错误:
Fatal error: Call to undefined method DateTime::createfromformat()
The error is happening at line 35. This is what my code reads from line 31 to 45
错误发生在第 35 行。这是我的代码从第 31 行到第 45 行读取的内容
// check database for necessary updates
$update = mysql_query("SELECT * FROM rent WHERE colour='#3C0'");
while($row_update = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between do nothing
} else {
// date is not between so update
$update_result = mysql_query("UPDATE rent SET colour='#F0F0F0' WHERE id=" . $row_update['id'] . " && colour='#3C0'");
mysql_close($update_result);
}
}
How can I resolve this?
我该如何解决这个问题?
回答by zerkms
DateTime::createFromFormat()was introduced in php 5.3. And most likely you have older one. So - install php >= 5.3 and you'll get it worked.
DateTime::createFromFormat()是在 php 5.3 中引入的。而且很可能你有一个旧的。所以 - 安装 php >= 5.3,你会得到它的工作。
回答by Mike Purcell
Which version of PHP are you running? According to PHP, createDateFormat
is available in versions >= 5.3.0.
您运行的是哪个版本的 PHP?根据PHP,createDateFormat
在版本 >= 5.3.0 中可用。
-- Edit
- 编辑
Looks like your code was using DateTime incorrectly, in that createFromFormat returns an object, not a string, but you should be able to transpose the DateTime::createFromFormat() calls with date() calls.
看起来您的代码错误地使用了 DateTime,因为 createFromFormat 返回一个对象,而不是一个字符串,但您应该能够将 DateTime::createFromFormat() 调用与date() 调用转置。
// PHP >= 5.3.0
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
// PHP < 5.3.0
$datetime_lower = date('d/m/Y', $min);
$datetime_upper = date('d/m/Y', $max);
$datetime_compare = date('d/m/Y g:i a', $row_update['pDate']);
It seems to me though, that if you are dealing with timestamps, you can do the comparison ops without having to convert to a specific format. If one of the dates you are dealing with isn't in a timestamp format you can do the following:
不过在我看来,如果您正在处理时间戳,则可以进行比较操作,而无需转换为特定格式。如果您处理的日期之一不是时间戳格式,您可以执行以下操作:
$timestamp = strtotime($yourFormattedDateTime);
// Now with everything in ints, you can do your conditional evals