I am trying to get only the month and year on this format (y-m) coming from y-m-d format of date. I am getting an error saying Call to a member function format() on a non-object. Am i doing it correctly? Thank you for your help with this.
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;
My apologies, my bad, the reason for the error was I put the following code inside the for loop which causes error. After moving the code outside the loop, the only thing I changed was the code from
$d = DateTime::createFromFormat("Y-m",$current_date);
to
$d = DateTime::createFromFormat("Y-m-d",$current_date);
Thanks alot!
Corrected code:
$current_date= '2017-04-03';
$d = DateTime::createFromFormat("Y-m-d",$current_date);
$current_date = $d->format("Y-m");
echo $current_date;