Generate in textbox a date calculated adding field DAYS and CURRENT DATE

0

I have a system of fines in which the days of fines must be placed and the date on which this fine starts and finishes, it happens that when the days are placed in a separate field it may happen that the number of days does not match the start and end dates, something like this:

Start date 23/2/17 days of fine 4 end date 29/2/17

I would like the start date to always be the current date in my system, and that the end date will be calculated by adding the days inserted in the days field, taking into account that the days start counting the day after the date of fine that is to say.

If you are fined on 2/23/17 and given 3 days, it would be: 24, 25 and 26, therefore, the deadline would be: 26-2-17.

On the other hand I would like to be able to save both dates in my database, because sometimes the format is different from MySQL

<html>
<head>
</head>
<body>
<form>
<h1>Indique los datos de la multa</h1>

<label>Ingrese fecha de inicio</label>
<input type="text" name="fecha_inicio" value="<?=$fecha_fin=date('d-m-Y');?>">

<br>
<label>Ingrese los días de arresto</label>
<input type="text" name="dias_arresto">

<br>
<label>Ingrese fecha de termino</label>
<input type="text" name="fecha_inicio" value="">

<br>

<input type="submit" value="procesar">
<?php
?>
</form>
</body>
</html>
    
asked by Victor Alvarado 23.02.2017 в 14:34
source

2 answers

0

I was researching and found the solution to add days to date and to self-manage, in addition to a button to reset the sum, since it is blocked to avoid being changed once calculated:

get_fechaactual.js

function fechaAtual() { 
    var fecha_actual = new Date() 
    var dia = fecha_actual.getDate() 
    var mes = fecha_actual.getMonth() + 1 
    var anio = fecha_actual.getFullYear() 

    if (mes < 10) 
        mes = '0' + mes 

    if (dia < 10) 
        dia = '0' + dia 
    return (dia + "/" + mes + "/" + anio) 
} 

function MostrarFechaA() {
   document.omd.fecha_inicio.value =  fechaAtual();
}  

get_fechatermino.js

function MostrarFechaT() {
 var d = document.getElementById('dias').value;
 if (d==0){
    alert("No has ingresado el numero de dias")
    var fechaFinal="";
    throw new FatalError();
 }else{
 var Fecha = new Date();
 var sFecha = fecha || (Fecha.getDate() + "/" + (Fecha.getMonth() +1) + "/" + Fecha.getFullYear());
 var sep = sFecha.indexOf('/') != -1 ? '/' : '-'; 
 var aFecha = sFecha.split(sep);
 var fecha = aFecha[2]+'/'+aFecha[1]+'/'+aFecha[0];
 fecha= new Date(fecha);
 fecha.setDate(fecha.getDate()+parseInt(d));
 var anno=fecha.getFullYear();
 var mes= fecha.getMonth()+1;
 var dia= fecha.getDate();
 mes = (mes < 10) ? ("0" + mes) : mes;
 dia = (dia < 10) ? ("0" + dia) : dia;
 var fechaFinal = (dia+sep+mes+sep+anno);
document.getElementById("fecha_termino").value=fechaFinal;
document.getElementById("dias").disabled = true;
document.getElementById("restablecer").hidden=false;
} 
}

function Restablecer() {
document.getElementById("fecha_termino").value="";
document.getElementById("dias").value="";
document.getElementById("dias").disabled = false;
document.getElementById("restablecer").hidden=true;
}

HTML:

<script src="funciones/obtener_fechaactual.js"></script>
  <script src="funciones/obtener_fechatermino.js"></script>
 <tr>
   <td>Dias: </td>
   <td>
   <input type="text" name="dias" onblur="MostrarFechaT()" id="dias">
   <input type="button" onclick="Restablecer()" id="restablecer" value="Cambiar" hidden>
   </td>
   <td><p><STRONG>NOTA:</STRONG>
   Las fechas de la OMD se calcularan automaticamente, despues de ingresar el n&uacute;mero de d&iacute;as.</p>
   </td>
   </tr>

   <tr>
   <td>Fecha de inicio: </td>
   <td><input type="text" name="fecha_inicio" readonly id="fecha_inicio" disabled></td>
   </tr>

   <tr>
   <td>Fecha de termino: </td>
   <td><input type="text" name="fecha_termino" readonly id="fecha_termino"></td>
   </tr>
    
answered by 10.03.2017 / 15:34
source
1

I think that what you are looking for can be solved with javascript, to generate the date and then with php (I suppose) to save in mysql.

This code will add a function to all variables of the type Date since we are using the prototype and also if you do this, the change of month or year is already handled, for example if you add 2 days to 31/10, then it will be 2/11.

Date.prototype.addDays = function(days) {
  var dat = new Date(this.valueOf());
  dat.setDate(dat.getDate() + days);
  return dat;
}

You can create a listener as an 'on change' when you select the start date, then create the object Date:

    var startDate = new Date();

And when you add the days of arrest you call the function:

    var newDate = startDate.addDays(numero_de_dias_a_agregar)

In this way you will already have in your new variable newDate the value of the date plus the number of days you want. Now you add the value of that date to the date field.

Now the other issue is when you send the data back, create variables of the PHP date type, docs: link , and in the MySql uses the type of timestamp field, docs: link , with that you can save your PHP variable in MySql. If you do this the format will not change because it is a Datestamp or Timestamp, and when you extract data from your mysql database you can create a Date object in PHP.

If you have any doubt, let me know! Greetings!

    
answered by 25.02.2017 в 08:47