Error in php v5.2.11 but not 5.3.5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cl2606
Forum Newbie
Posts: 9
Joined: Thu Jul 07, 2011 12:40 pm

Error in php v5.2.11 but not 5.3.5

Post 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).
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Error in php v5.2.11 but not 5.3.5

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Error in php v5.2.11 but not 5.3.5

Post 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);
cl2606
Forum Newbie
Posts: 9
Joined: Thu Jul 07, 2011 12:40 pm

Re: Error in php v5.2.11 but not 5.3.5

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Error in php v5.2.11 but not 5.3.5

Post 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;
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Error in php v5.2.11 but not 5.3.5

Post by twinedev »

good to know. I've not done much with classes in PHP yet, just basics. Thanks for that info!
Post Reply