Adjusting code that was meant for PHP 5.3 to fit in with PHP 5.2.17

197 Views Asked by At

My server keeps returning the error because the function is not supported. I have recently found out that I cannot upgrade to PHP 5.3 until Zend Optimizer supports it; at the moment our server admins want to stick to the stable version of ZO for the server environment.

Can you recommend any adjustments that I can make to the code?

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

This is the code:

$today = date('l');

    if($today == 'Wednesday'){
        $min = date('d/m/Y', strtotime('0 days'));
        $max = date('d/m/Y', strtotime('+6 days'));
    }else if($today == 'Thursday'){
        $min = date('d/m/Y', strtotime('-1 days'));
        $max = date('d/m/Y', strtotime('+5 days'));
    }else if($today == 'Friday'){
        $min = date('d/m/Y', strtotime('-2 days'));
        $max = date('d/m/Y', strtotime('+4 days'));
    }else if($today == 'Saturday'){
        $min = date('d/m/Y', strtotime('-3 days'));
        $max = date('d/m/Y', strtotime('+3 days'));
    }else if($today == 'Sunday'){
        $min = date('d/m/Y', strtotime('-4 days'));
        $max = date('d/m/Y', strtotime('+2 days'));
    }else if($today == 'Monday'){
        $min = date('d/m/Y', strtotime('-5 days'));
        $max = date('d/m/Y', strtotime('+1 days'));
    }else if($today == 'Tuesday'){
        $min = date('d/m/Y', strtotime('-6 days'));
        $max = date('d/m/Y', strtotime('0 days'));
    }

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

3

There are 3 best solutions below

1
On

Try to replace DateTime::createFromFormat('d/m/Y', $xxx); with date('d/m/Y', strtotime($xxx));.

1
On

PHP <= 5.3 is no longer supported, so do try to convice your admins for upgrade ( let them choose - stable Zend Optimizer or stable PHP Interpreter - which is more critical? )

0
On

You could replace that big if/elseif block with this:

$today = date("w");
$min_mod = ($today < 3) ? -4 - $today : 3 - $today;
$max_mod = ($today < 3) ? 2 - $today : 9 - $today;
$min = strtotime("today 00:00:00 -".$min_mod." days");
$max = strtotime("today 23:59:59 +".$max_mod." days");

then below in your SQL loop:

//$datetime_lower   = DateTime::createFromFormat('d/m/Y', $min);
//$datetime_upper   = DateTime::createFromFormat('d/m/Y', $max);
$compare = strtotime($row_update['pDate']);
if ($min < $compare && $max > $compare) {
    // date is between do nothing
} else {
    // code snipped
}