Page 1 of 1

Error in php v5.2.11 but not 5.3.5

Posted: Sat Jul 16, 2011 2:05 pm
by cl2606
I'm getting the following error:
[text]Fatal error: Call to a member function format() on a non-object[/text]

because of the following lines:

Code: Select all

          $last_due_date = new DateTime($last_payment_history['next_due']);
           $interval_string = "+".$num_weeks." weeks";
          // calculate the next due date based on the last due date recorded
          
          $next_due_date = $last_due_date->modify($interval_string);    
        
          // put the date into MySQL format      
          $next_due_date_f = $next_due_date->format($date_format);
The error is hitting on the last line: $next_due_date_f = $next_due_date->format($date_format) I get the error on V5.2.11 but not on V3.5.3.

Any ideas how I can get around this? I'm using 5.2.11 because my web host is using 5.2.15. (I'm using 5.2.11 because that is the highest 5.2.x version I can get on WAMP and I figured if it runs on 5.2.11 it will run on 5.2.15).

Re: Error in php v5.2.11 but not 5.3.5

Posted: Sat Jul 16, 2011 2:19 pm
by social_experiment

Code: Select all

<?php
$next_due_date = $last_due_date->modify($interval_string);    
        
// put the date into MySQL format      
$next_due_date_f = $next_due_date->format($date_format);
?>
The format() method is being called on a non-object. I might be wrong but i think you have to create a new instance of an object before you can use it in php 5.
The Manual wrote:To create an instance of a class, a new object must be created and assigned to a variable.

Re: Error in php v5.2.11 but not 5.3.5

Posted: Sat Jul 16, 2011 2:34 pm
by twinedev
From: http://www.php.net/manual/en/datetime.modify.php in the changelog section:
5.3.0 - Changed the return value on success from NULL to DateTime.
So in 5.2.11, you are assigning NULL to the variable $next_due_date

-Greg

PS. In looking at this, you can try this for backwards compatibility:

Code: Select all

$next_due_date = $last_due_date;
$next_due_date->modify($interval_string);

Re: Error in php v5.2.11 but not 5.3.5

Posted: Sat Jul 16, 2011 3:21 pm
by cl2606
Thanks for the responses.

Greg - It worked! Thank you.

I knew I was getting a NULL value because it was setting to today's date, but I couldn't figure out the correct syntax.

Thanks again guys.

Curt

Re: Error in php v5.2.11 but not 5.3.5

Posted: Sun Jul 17, 2011 12:49 pm
by McInfo
twinedev wrote:

Code: Select all

$next_due_date = $last_due_date;
$next_due_date->modify($interval_string);
$next_due_date now points to the same object as $last_due_date. There are two names, but one object. To make a copy, use the clone keyword.

Code: Select all

$next_due_date = clone $last_due_date;

Re: Error in php v5.2.11 but not 5.3.5

Posted: Sun Jul 17, 2011 2:25 pm
by twinedev
good to know. I've not done much with classes in PHP yet, just basics. Thanks for that info!