I wrote this piece of code
echo date("Y-m-d", strtotime($date, strtotime("+ " . $days . " days")));
$date = 2012-04-12
$days = 15
I am looking to add $days
(15 days) to the date (2012-04-12
) I am expecting to get 2012-04-27
and this code returns me 2012-04-12
, what am I doing wrong?
echo date('Y-m-d', strtotime('+15 days', strtotime('2012-04-12')));
Don't use strtotime(). It's unreliable, though you're not really doing anything that would really come back to bite you in the rump. But depending on strtotime is still a bad thing, because it WILL make your life miserable at some point.
Use DateTime instead:
$now = DateTime::CreateFromFormat('Y-m-d', '2012-04-12');
$then = $now->add(new DateInterval('P15D'));
$date = $then->format('Y-m-d');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With