Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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

// 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?

like image 384
methuselah Avatar asked Jan 05 '12 20:01

methuselah


2 Answers

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.

like image 129
zerkms Avatar answered Nov 04 '22 17:11

zerkms


Which version of PHP are you running? According to PHP, createDateFormat is available in versions >= 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.

// 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
like image 32
Mike Purcell Avatar answered Nov 04 '22 16:11

Mike Purcell