Auto-increment in input type date in php with codeigniter

0

Hello!

I need help for something that has been around my head for a couple of days and I can not see how to even start (I know, it sounds pretentious, but we all have passed sometime in this life).

What happens is that I have 3 input's of date (date1, date2 and date3) .

With them I would like to make that when I put a date in the first input:

The others are added automatically in 3 days and the one that follows that adds up to 30 days (Not that it adds 33 days, it would have to be separately, add the 30 days it has of the input "date2" because if it is modified this (date2), the input "date3" must change to 30 more days). To simplify:

I've been looking at how to achieve the task, and at the time, I read that it could work with the strtotime function but I do not know how that function would be (I'm nood and I try to learn every day, my teachers do not teach me enough) I hope not to sound selfish or something similar, but really, I want to learn more.

I'm working with Bootstrap on codeigniter, for this case, I think it would be more with JQUERY, right?

    
asked by González 19.12.2017 в 06:02
source

1 answer

1

Use the Moment.js library to manipulate dates.

As you type in the fecha 1 check if the format of the date is valid. When it is, load the value for fecha 2 and fecha 3 . If you modify the fecha 2 the fecha 3 is modified. The problem is that if you type again in fecha 1 , the other two dates are modified. On the other hand, I do not know if you have to be able to modify the fecha 3 by hand or not.

If it does not exactly meet what you need, let me know and we'll modify it.

$('#fecha1').change(function() {
  var fecha1 = $(this).val();
  var time = moment(fecha1, 'YYYY-MM-DD', true);
  if (time.isValid()) {
    var fecha2 = time.add(3, 'd');
    $('#fecha2').val(fecha2.format('YYYY-MM-DD'));
    var fecha3 = fecha2.add(30, 'd');
    $('#fecha3').val(fecha3.format('YYYY-MM-DD'));
  }
});

$('#fecha2').change(function() {
  var fecha2 = $(this).val();
  var time = moment(fecha2, 'YYYY-MM-DD', true);
  if (time.isValid()) {
    var fecha3 = time.add(30, 'd');
    $('#fecha3').val(fecha3.format('YYYY-MM-DD'));
  }
});
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="row">
    <div class="form-group col">
      <label for="fecha1">Fecha 1</label>
      <input type="date" class="form-control" id="fecha1" placeholder="dd/mm/aaaa">
    </div>
    <div class="form-group col">
      <label for="fecha2">Fecha 2</label>
      <input type="date" class="form-control" id="fecha2" placeholder="dd/mm/aaaa">
    </div>
    <div class="form-group col">
      <label for="fecha3">Fecha 3</label>
      <input type="date" class="form-control" id="fecha3" placeholder="dd/mm/aaaa">
    </div>
  </div>
</div>
    
answered by 19.12.2017 / 07:02
source