Add days to a date


I need to add another 2 days on a date from a variable.

I'm doing it this way but it's not working.

$data = '17/11/2014';
echo date($data, strtotime("+2 days"));
asked by anonymous 17.11.2014 / 14:38

3 answers


I've already been able to do what I wanted. I'll leave the code here for others to use.

$data = "20-10-2014"; 
echo date('d/m/Y', strtotime("+2 days",strtotime($data))); 
17.11.2014 / 14:42

Another way to accomplish this is to use the DateTime class and pass the period to be added to date, it is specified in the DateInterval constructor, it should start with P (period) and followed from a last number to the unit:

Y | Ano
M | Mês
D | Dias
W | Semanas
H | Horas
M | Minutos
S | Segundos  

Note that month and minute use the same letter M . In order for the parse to be done correctly the larger unit (s) should be on the left.

For values that contain hours, minutes, or seconds, you must use the letter T to signal this passage.


$data = '17/11/2014';

$data = DateTime::createFromFormat('d/m/Y', $data);
$data->add(new DateInterval('P2D')); // 2 dias
echo $data->format('d/m/Y');


17.11.2014 / 14:49

Your problem is with strtotime . You need to say over what you want to add 2 days. You were only adding 2 days to nothing. In addition, you need to specify the format of the new date so that you do not risk going out in an undesirable way.

$data = "17/11/2014";
echo date('d/m/Y', strtotime($data. ' + 2 days'));

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

17.11.2014 / 14:46