Change status according to date

Asked

Viewed 123 times

0

I have tremendous doubt about how I could solve/create the solution. I could do it manually by creating a select on the side and changing the status of the race after Ubmit, but the idea is as follows: As I can create a condition, be it in the back-end/php that upon reaching the mentioned date change/fill the STATUS to "RACE MADE".

My bank exists the 2 fields below:

In the comic book:
date type: varchar;
status: varchar;

Html fields:

<div class="form-group col-md-3">
    <label><b>1° - Data e Hora</b></label>
    <input type="datetime-local" name="dt_corrida1" id="data_corrida" 
        class="form-control" value="<?php echo $row[0]['dt_corrida1']; ?>"  
        placeholder="Data Corrida">
</div>
<div class="form-group col-md-3">
    <label><b>STATUS</b></label>
    <input type="text" name="status" id="status" class="form-control" 
        value="<?php echo $row[0]['status']; ?>">
</div>

inserir a descrição da imagem aqui

EDIT: Image of how it’s in the database. Type is not like datetime because I couldn’t bring the input information with type "datetime-local", only with varchar. inserir a descrição da imagem aqui

Information recorded in the BD: inserir a descrição da imagem aqui

INPUTS:

DATE RACE COMING FROM THE COMIC PULLING FROM Row.

<input type="datetime-local" name="dt_corrida1" id="data_corrida" class="form-control" value="<?php echo $row[0]['dt_corrida1']; ?>"  placeholder="Data Corrida">

STATUS:

<input readonly type="text" name="status" id="status" class="form-control" value="">

EDIT IMAGE OF INPUTS AND STATUS - I changed the ids that was requested and js replaces as edited. inserir a descrição da imagem aqui

Thanks in advance!

1 answer

2


Dai you add not to lose input from data_corrida, I believe you’ve done it in $row[0]['dt_corrida1'];, but finally makes a function that when loading it is executed. The model where the input type is located is YEAR-MONTH-HOUR:MINUTES, dai takes the current year, the current month, the current day, the current time and the current minutes, and turns everything into a string of the same model that is in the input, compares and when they are equal appears the "RUN MADE". A and added the tag readonly in the status input, for those who are accessing not being able to type in it.

Editing: Use the slice() to "cut out" the string and pick up the year, month, day... and makes when the current date is higher appear the "RUN MADE", and if the date has not yet arrived the status is empty.

function compararData(){
var data = new Date();
var month = data.getUTCMonth() + 1;
var day = data.getUTCDate();
var year = data.getUTCFullYear();
var hour = data.getHours();
var min = data.getMinutes();
if (min < 10) {
   min = "0" + min
}
if (day < 10){
    day = "0" + day
}
if (month < 10){
    month = "0" + month
}

agora = year + "-" + month + "-" + day + "T" + hour + ":" + min

for (let i =1; i<=15; i++){
dataCorrida = document.getElementById('data_corrida'+i)
StatusCorrida =  document.getElementById('status'+i)
  
    if (dataCorrida.value.length >= 16) {
    if (dataCorrida.value == agora){
        StatusCorrida.value = "CORRIDA EFETUADA"
    }
    if (year > dataCorrida.value.slice(0,4)){
            StatusCorrida.value = "CORRIDA EFETUADA"
    }
    if (dataCorrida.value.slice(0,4) == year && month > dataCorrida.value.slice(5,7)){
            StatusCorrida.value = "CORRIDA EFETUADA"
    }
    if (dataCorrida.value.slice(0,4) == year && dataCorrida.value.slice(5,7) == month && day > dataCorrida.value.slice(8,10)){
            StatusCorrida.value = "CORRIDA EFETUADA"
    }
    if (dataCorrida.value.slice(0,4) == year && dataCorrida.value.slice(5,7) == month && dataCorrida.value.slice(8,10) == day && hour > dataCorrida.value.slice(11,13)) {
           StatusCorrida.value = "CORRIDA EFETUADA"
    }
    if (dataCorrida.value.slice(0,4) == year && dataCorrida.value.slice(5,7) == month && dataCorrida.value.slice(8,10) == day && dataCorrida.value.slice(11,13) == hour && min > dataCorrida.value.slice(14,16)) {
           StatusCorrida.value = "CORRIDA EFETUADA"
    }
    }
    }
    setInterval(compararData,500)
}
<body onload="compararData()">
    <div class="form-group col-md-3">
        <label><b>1° - Data e Hora</b></label>
        <input type="datetime-local" name="dt_corrida1" id="data_corrida1" class="form-control"  placeholder="Data Corrida">
    </div>
<div class="form-group col-md-3">
        <label><b>STATUS</b></label>
        <input readonly type="text" name="status" id="status" class="form-control" value="">
    </div>
    
</body>

NOTE: I removed the snippets in PHP just for you to test there in Stack

hope I’ve helped

Browser other questions tagged

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