Fatal error: call to undefined method DateTime :: createfromformat ()

How do you solve the error:

Fatal error: Call to undefined method DateTime::createfromformat() 

The error occurs on line 35. This is what my code reads from lines 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 solve this?

+7
source share
2 answers

DateTime :: createFromFormat () was introduced in php 5.3. And most likely you have an older one. So set php> = 5.3 and you will get it working.

+20
source

What version of PHP are you running in? According to PHP , createDateFormat is available in versions> = 5.3.0.

- Change

It looks like your code misused DateTime, since createFromFormat returns an object, not a string, but you have to transfer DateTime :: createFromFormat () calls from 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 that if you are dealing with timestamps, you can make ops comparisons without having to convert to a specific format. If one of the dates you are working with is not in the timestamp format, you can do the following:

 $timestamp = strtotime($yourFormattedDateTime); // Now with everything in ints, you can do your conditional evals 
+5
source

All Articles