Jump to content
jakebur01

DateTime could not be converted to string

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");

 

Share this post


Link to post
Share on other sites

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);

 

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Posted (edited)
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

Share this post


Link to post
Share on other sites

I suspect this is the code causing the error

// do other tasks

 

Share this post


Link to post
Share on other sites
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);

 

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites
3 minutes ago, Barand said:

Post the full error message too

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Posted (edited)

I added "new" back in.  Same error. 

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

Edited by jakebur01

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.