Value of the date is changing month by day

Asked

Viewed 226 times

-2

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

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

http://ccmbr.com.br/ajaxDatas/

<?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>';
  • Be clearer about your doubt. What do you need? What have you tried to do?

  • Day 10 + 7 = 17

  • where is the script that calculates new date?

  • <?php $days = $_POST['Duration']; $dateInitial = $_POST['stardate']; $dateFinal = date(’d/m/Y', strtotime("+$days days",strtotime($stardate)); $dateInitil = date(’d/m/Y', strtotime("+0 days",strtotime($stardate)); echo '</br>'. '</br>'; echo "start date". $startData; echo '</br>'. '</br>'; echo "+ " . $days." days "; echo '</br>'. '</br>'; echo "final date". $dataFinal. '</br>';

  • If any answer has solved your problem mark it as aceita see how and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

3 answers

3

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

1 - Save sent form date in a variable $dataInicial;

$dataInicial = $_POST['dataInicial'];

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

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

3 - Possession of these components above, we will use them in function mktime to get the timestamp (number of seconds since 01/01/1970) of the date already sent adding the number of days coming from the form ($dia + $dias) for the subsequent obtaining of the final date.

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

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

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

Complete 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 only one operation

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

strftime - Format a time/date according to 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 - reset the current time of the Datetime object to a different time

Modify - changes the timestamp of a Datetime object by incrementing or decreasing it


Using the class Dateinterval

$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

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>';

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.