22
I need to add 2 more days on a date coming from a variable.
I’m doing it this way but it’s not working.
$data = '17/11/2014';
echo date($data, strtotime("+2 days"));
22
I need to add 2 more days on a date coming from a variable.
I’m doing it this way but it’s not working.
$data = '17/11/2014';
echo date($data, strtotime("+2 days"));
33
I’ve already done what I wanted to do. I’ll leave the code here for other people to use.
$data = "20-10-2014";
echo date('d/m/Y', strtotime("+2 days",strtotime($data)));
13
Your problem is in the use of strtotime()
. You need to say on top of that you want to add up 2 days. You were just adding 2 days to nothing. In addition you need to specify in which format you want the new date to not run the risk of exiting in an unwanted way.
$data = "17/11/2014";
echo date('d/m/Y', strtotime($data. ' + 2 days'));
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Buddy, thank you.
13
Another way to accomplish this task is to use the class Datetime and pass the period to be added to the date, it is specified in the Dateinterval, should start with P
(period) and followed by a unit number:
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
. So that the parse is done correctly the larger(s) unit(s) must be on the left.
For values containing hours, minutes or seconds use the letter T
to signal this section.
<?php
$data = '17/11/2014';
$data = DateTime::createFromFormat('d/m/Y', $data);
$data->add(new DateInterval('P2D')); // 2 dias
echo $data->format('d/m/Y');
Friend, thank you. I’ll do it this way that you passed.
@Lucaswilliam, if your php version is 5.3 or higher prefer to use the Datetime class, otherwise use the functions, date, strtotime etc.
Browser other questions tagged php date
You are not signed in. Login or sign up in order to post.
Guys, I’ve already done what I wanted to do. I’ll leave the code here for other people to use. $data = "20-10-2014"; echo date(’d/m/Y', strtotime("+2 days",strtotime($data)));
– Lucas William
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero