I need to add days to given string date and display calculated date in string
This is what I have tried, but I could not make it work.
$date = date_create('1-Feb-2012');
$newDate = date_modify($date, '+2 day');
echo 'Your date is' . $newDate . '.';
This gives an error
Object of class DateTime could not be converted to string
You need to tell the
DateTimeobject how to format its output usingDateTime::format. So, for example:Also note that
modifydirectly modifies theDateTime– it doesn’t just return a new one, as the documentation might lead you to believe – so I’ve removed the second variable. I’ve taken the liberty of changing the objects to the object-oriented form as well, which you should be using 🙂Here’s a working demo.