Convert date MMMM-YYYY to dd/mm/yyyy datepicker bootstrap

Asked

Viewed 508 times

9

I have the following date field:

<div class='input-group date input-data' id='datetimepicker4'>
    <input type='text' name="mes" id="mes" class="form-control hidden" />
    <span class="input-group-addon hidden">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div> 

 $('#datetimepicker4').datetimepicker({
    format: 'MMMM-YYYY',
    locale: 'pt-br',
    defaultDate: new Date() 
});

In this format is displayed: September-2016. How to display in this format and then convert to 01/09/2016?

  • Tried using an option called formatDate: 'Y/m/d' ?

  • On the server, who will process this information? PHP, Java... ?

  • 2

    It happened to me and I had to use the plugin moment to solve my problem by selecting a date.

  • @Kennyrafael php

  • 1

    In this case it would be possible to send as in the view and treat on the side in PHP or you want to treat already on the screen for the user?

  • Because it would display both ways, but I believe that dealing with PHP already solves even.

  • 1

    A variation of bootstrap datetimepicke, I found very complete, https://uxsolutions.github.io/bootstrap-datepicker/

Show 2 more comments

1 answer

2

In PHP, we can create a function called converterData, and in it return the month and year in the format you want.

<?php

$data = "setembro-2016";

function converterData($data) {
  $meses = Array(
    "janeiro" => "01",
    "fevereiro" => "02",
    "março" => "03",
    "abril" => "04",
    "maio" => "05",
    "junho" => "06",
    "julho" => "07",
    "agosto" => "08",
    "setembro" => "09",
    "outubro" => "10",
    "novembro" => "11",
    "dezembro" => "12"
  );
  $data = explode("-", $data);
  $mes = $meses[$data[0]];
  $ano = $data[1];
  return "01/$mes/$ano";
}

print converterData($data);
# -> 01/09/2016

?>

Browser other questions tagged

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