Jump to content

Recommended Posts

I am preparing $begin and $end for DatePeriod() but I can't get passed this error "getting the error Recoverable fatal error: Object of class DateTime could not be converted to string".

$today = date("Y-m-d");
$datestop=$_POST['datestop'];
//echo "$today<br /><br />$datestop<br /><br />";
$begin = new DateTime("$today");
$end = new DateTime("$datestop");

 

I see nothing in that code that would give that error.

FYI. don't put a single string variable inside quotes as you have 

$end = new DateTime("$datestop");
                    ^         ^

It's inefficient and unnecessary. Just use

$end = new DateTime($datestop);

 

2 hours ago, Barand said:

I see nothing in that code that would give that error.

FYI. don't put a single string variable inside quotes as you have 


$end = new DateTime("$datestop");
                    ^         ^

It's inefficient and unnecessary. Just use


$end = new DateTime($datestop);

 

I originally did not have quotes, but added them in to see if it would make any difference.  The error goes away when I comment out these two lines.

$today and $datestop returns:  I can't see anything wrong with it yet either.

2019-10-08

2024-10-08

1 hour ago, cyberRobot said:

How are you using $begin and/or $end later in the script? If you're outputting the value stored in the DateTime instance, you could use the format method:
https://www.php.net/manual/en/datetime.format.php

 

 

$today = date("Y-m-d");
$datestop=$_POST['datestop'];
//echo "$today<br /><br />$datestop<br /><br />";
$begin = new DateTime($today);
$end = new DateTime($datestop);

$interval = DateInterval::createFromDateString($interval);
$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {

$dateloop = $dt->format("Y-m-d");
// do other tasks

}

CyberRobot, this is how I'm using it.

Edited by jakebur01
14 minutes ago, Barand said:

I suspect this is the code causing the error


// do other tasks

 

I can comment out the whole page and the error doesn't go away until I comment out:

$begin = DateTime($today);
$end = DateTime($datestop);

 

Ok, this is the only code that is not commented out:

if(isset($_POST['datestop'])){

$today = date("Y-m-d");
$datestop=$_POST['datestop'];
echo "$today<br /><br />$datestop<br /><br />";

$begin = DateTime($today);
$end = DateTime($datestop);

}

Fatal error: Uncaught Error: Call to undefined function DateTime().  Stack trace: #0 {main} thrown

Edited by jakebur01
9 minutes ago, jakebur01 said:

$begin = DateTime($today);

$end = DateTime($datestop);

You have removed the "new"s

Should be

$begin = new DateTime($today);
$end   = new DateTime($datestop);

BTW error messages have line numbers to tell you where it happened.

I ran

if(isset($_POST['datestop'])){

$today = date("Y-m-d");                                
$datestop=$_POST['datestop'];                     // assuming $datestop now contains '2019-10-15'
echo "$today<br /><br />$datestop<br /><br />";

$begin = new DateTime($today);
$end = new DateTime($datestop);

}

 - no errors!

then added

$period = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach ($period as $d) {
    echo $d->format('jS F Y') . "<br>\n";
}

giving

Quote

8th October 2019
9th October 2019
10th October 2019
11th October 2019
12th October 2019
13th October 2019
14th October 2019

 

I am curious about this line that you posted earlier. (You don't show us where you set that $interval value)

$interval = DateInterval::createFromDateString($interval);

Immediately before that line put

var_dump($interval);

and post the output. (You don't show us where you set that value)

55 minutes ago, Barand said:

I suspect this is the code causing the error


// do other tasks

 

You were right, I had another variable named $end that was causing the problem.  I renamed my start and end to datebegin and dateend and it fixed the issue.  Thank you.

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.