php - Input Date format Produce Error: Object of class DateTime could not be converted to string -
i trying increment 1 month given date , receiving following error while converting;
note: kindly check comment in code.
catchable fatal error: object of class datetime not converted string
my code:
$start_date = '25-05-2015'; // 25th of may 2015 $dateval = explode("-",$start_date); $newdate = $dateval[1].'-'.$dateval[0].'-'.$dateval[2].' 00:00:00'; // converted $cal_date = datetime::createfromformat('m-d-y h:i:s', $newdate); $date = new datetime($cal_date); // give standard format input above date calculations made. pls specify useful method? $interval = new dateinterval('p1m'); $date->add($interval); $currentdate = $date->format('y-m-d'); how can convert date find date after 1 month?
just reuse $cal_date, no need feed new $date object it:
$start_date = '25-05-2015'; // 25th of may 2015 $dateval = explode("-",$start_date); $newdate = $dateval[1].'-'.$dateval[0].'-'.$dateval[2].' 00:00:00'; // converted $cal_date = datetime::createfromformat('m-d-y h:i:s', $newdate); $interval = new dateinterval('p1m'); $cal_date->add($interval); $currentdate = $cal_date->format('y-m-d'); echo $currentdate; the problem is, you're trying feed datetime object instead of string. says in error:
$cal_date = datetime::createfromformat('m-d-y h:i:s', $newdate); // ^ date time object $date = new datetime($cal_date); // feeding datetime object constructor or why not directly set format in place, remove part wherein you're exploding. don't need those:
$start_date = '25-05-2015'; $cal_date = datetime::createfromformat('d-m-y h:i:s', $start_date . ' 00:00:00'); $interval = new dateinterval('p1m'); $cal_date->add($interval); $currentdate = $cal_date->format('y-m-d'); echo $currentdate;
Comments
Post a Comment