Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date strtotime not working

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?

like image 441
user979331 Avatar asked Apr 12 '12 18:04

user979331


2 Answers

echo date('Y-m-d', strtotime('+15 days', strtotime('2012-04-12')));
like image 114
Nate Avatar answered Sep 28 '22 01:09

Nate


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');
like image 20
Marc B Avatar answered Sep 28 '22 00:09

Marc B