jakebur01 Posted October 8, 2019 Share Posted October 8, 2019 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"); Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 2019 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); Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 8, 2019 Share Posted October 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 (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 October 8, 2019 by jakebur01 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 2019 I suspect this is the code causing the error // do other tasks Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 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); Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 (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 October 8, 2019 by jakebur01 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 2019 Post the full error message too Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 2019 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. Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 (edited) I added "new" back in. Same error. Fatal error: Uncaught Error: Call to undefined function DateTime(). Stack trace: #0 {main} thrown Edited October 8, 2019 by jakebur01 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 2019 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2019 Share Posted October 8, 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) Quote Link to comment Share on other sites More sharing options...
jakebur01 Posted October 8, 2019 Author Share Posted October 8, 2019 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.