Jump to content

date_create - Fatal error: Call to a member function format() on a non-object in


Recommended Posts

Hey guys,

 

I have spent the last 36 hours trying to figure this out.

 

I keep getting the following error:

 

Fatal error: Call to a member function format() on a non-object in /var/www/vhosts/xxxxx.com/httpdocs/admin/defaults.php on line 51

 

The reason I am not using the standard date and strtotime functions is because some of my dates exceed 2038.

 

What I have done is created an array with price and days from today within the array.

 

You can see below where I am trying to get the timestamp for 14000 days from today. I then turn that into a date using the newdate function.

 

I can see that this actually works on other pages on my server, I just can't figure out why it won't work here. BTW, I am using php 5.2.3.

 


$csv .= "Payment,".newdate("m/d/Y",newstrtotime("+".$periodic[0][0]." days")).",".$row['amount'].",".count($periodic).",".$row['frequency'].",".newdate("m/d/Y",newstrtotime("+".$periodic[$perCt][0]." days"))."\n";
function newstrtotime($strtotime){
$datetime = date_create($strtotime);
return $datetime->format("U");
}        
function newdate($format,$timestamp){
$datetime = date_create("@$timestamp");
return $datetime->format($format);
}

 

 

Let me know what you think.

 

Hi,

 

Your "$strtotime" variable probably is not a correct format for DateTime.

 

$csv .= "Payment,".newdate("m/d/Y",newstrtotime("+".$periodic[0][0]." days")).",".$row['amount'].",".count($periodic).",".$row['frequency'].",".newdate("m/d/Y",newstrtotime("+".$periodic[$perCt][0]." days"))."\n";
function newstrtotime($strtotime){
try
{
    $datetime = new DateTime($strtotime);
    return $datetime->format("U");
}
catch (Exception $e)
{
   // echo $e->getMessage(); // if you wanna know the error message
    return NULL;
}
...

 

If you use the procedural constructor you cannot catch any exceptions. So thats why the OOP's mode.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.