Jump to content

DateTime could not be converted to string


jakebur01
 Share

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

 

Link to comment
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

Link to comment
Share on other sites

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

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
Link to comment
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

 

Link to comment
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.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.