How can I make an elapsed date calculation

0

basically I have a date type imput where the user places the date in which I joined the company, after that I want to show in other input the elapsed time (basically something like this: 1 year 3 months, 0 year 6 months ), To the current date. I would like to do this with php and ajax but I do not know how to do it

    
asked by lesther gonzalez 09.05.2018 в 01:26
source

1 answer

0

Use Ajax and PHP only in case you really need to interact with the server, for example, if you need to save such a difference in a database.

In the case of, simply wanting to calculate the difference, the most practical and efficient way to do it would be using Javascript to obtain the difference between the selected time and the current one.

To launch such a function, you need to enter an event onchange = 'return check_date ()' in the date field and a javascript function such that:

function comprobar_fecha(){
    var fecha = new Date(document.getElementByID('fecha_form').value);
    var fecha_actual = new Date();
    var diferencia = fecha_actual - fecha; 
    var diferenciaDias = Math.ceil(diferencia/ (1000 * 3600 * 24));  // Paso la diferencia a horas
    document.getElementById('horas_trabajadas').value = diferenciaHoras;
}
    
answered by 09.05.2018 в 01:49