Date value is changing month by day

-2
<input type="text" id="inicio" class="cad-input" name='dataInicial' value="<?php echo date('d/m/Y'); ?>"  style="width: 190px;"></> ,

The variable is in the format date('d/m/Y') and is changing to date('m/d/Y')

link

<?php 
$dias = $_POST['duration']; 
$dataInicial = $_POST['dataInicial']; 
$dataFinal = date('d/m/Y', strtotime("+$dias days",strtotime($dataInicial))); 
$dataInicial = date('d/m/Y', strtotime("+0 days",strtotime($dataInicial))); 
echo '</br>'.'</br>'; 
echo "data inicial ".$dataInicial; 
echo '</br>'.'</br>'; 
echo "+ " .$dias." dias "; 
echo '</br>'.'</br>'; 
echo "data final ".$dataFinal. '</br>';
    
asked by anonymous 10.10.2017 / 17:58

3 answers

3
  

Using mktime () which is useful for arithmetic and date validation.

1 - We saved the sent date of the form in a variable $dataInicial ;

$dataInicial = $_POST['dataInicial'];

2 - We get the components of this date with the function list() .

list($dia, $mes, $ano) = explode('/', $dataInicial);

3 - In possession of these components above, we will use them in the mktime function to get the timestamp (number of seconds from 01/01/1970) of the date sent already adding the number of days coming from the form $dia + $dias ) for later retrieval of the end date.

$time = mktime(0, 0, 0, $mes, $dia + $dias, $ano);

4 - We apply the function strftime to show the timestamp in the desired format

$dataFinal = strftime('%d/%m/%Y', $time);

Full Code

$dias        = $_POST['duration'];
$dataInicial = $_POST['dataInicial'];


//obtendo os componentes da sua data.
list($dia, $mes, $ano) = explode('/', $dataInicial);

//usando mktime para obter o timestamp desejado (somando o numero de dias)
$time = mktime(0, 0, 0, $mes, $dia + $dias, $ano);

//strftime para mostrar o timestamp em formato desejado
$dataFinal = strftime('%d/%m/%Y', $time);

echo '</br>'.'</br>';
echo "data inicial ".$dataInicial;
echo '</br>'.'</br>';
echo "+ " .$dias." dias  ";
echo '</br>'.'</br>';
echo "data final ".$dataFinal. '</br>';

Example - ideone

- list () is used to create a list of variables in just one operation

- mktime - get a Unix timestamp of a date . This timestamp is a long integer containing the number of seconds between Unix Era (January 1 1970 00:00:00 GMT), and the specified time

- strftime - Formats a time / date according with local settings

Using the modify method

$dias        = $_POST['duration'];
$dataInicial = $_POST['dataInicial'];

// Criar o objeto representando a data
$obj_data = DateTime::createFromFormat('d/m/Y', $dataInicial);

// Definir a hora, minuto e segundo, que não foram informados
// (caso contrário, é obtido os valores da hora atual)
$obj_data->setTime(0, 0, 0);

// Realizar a soma de $dias dias
$obj_data->modify("+$dias days");

// Formatar a data obtida
echo $obj_data->format('d/m/Y');

Example - ideone

DateTime :: createFromFormat - returns a new DateTime object formatted according to an informed format

setTime - resets the current time of the DateTime object to a different one

modify - changes the timestamp of a DateTime object, or decreasing it

Using the DateInterval class

$dias        = $_POST['duration'];
$dataInicial = $_POST['dataInicial'];

// Criar o objeto representando a data
$obj_data = DateTime::createFromFormat('d/m/Y', $dataInicial);
$obj_data->setTime(0, 0, 0);

$intervalo = new DateInterval("P{$dias}D");
$obj_data->add($intervalo);

// Formatar a data obtida
echo $obj_data->format('d/m/Y');

Example - ideone

DateInterval - creates a new DateInterval object

add - adds a number of days, months, years, hours, minutes, and seconds of a DateTime object

    
10.10.2017 / 19:48
0
<?php
$dias        = $_POST['duration'];
$dataInicial = $_POST['dataInicial'];
$dataFinal   = date('d/m/Y', strtotime("+$dias days",strtotime($dataInicial)));
$dataInicial = date('d/m/Y', strtotime("+0 days",strtotime($dataInicial)));
echo '</br>'.'</br>';
echo "data inicial ".$dataInicial;
echo '</br>'.'</br>';
echo "+ " .$dias." dias  ";
echo '</br>'.'</br>';
echo "data final ".$dataFinal. '</br>';
    
10.10.2017 / 18:44
-1

Just do

 <input type="text" placeholder="Ex.: 00/00/0000" autocomplete="off" maxlength="10" value="<?php echo date('d/m/Y', strtotime(" +x days"));?>">

where x will be your selected value variable

    
10.10.2017 / 20:16