Make date range datepicker be used in mysql

0

In my code, I have a calendar where the user selects the period that he wants to analyze. The code is this:

<input type="text" name="daterange" value="01/01/2012 - 20/04/2018" />
<script type="text/javascript">
$(function() {
$('input[name="daterange"]').daterangepicker({
    timePicker: false,
    timePickerIncrement: 0,
    locale: {
        format: 'DD/MM/YYYY'
    }
});
});
</script>

I need to use this range to filter a mysql data table. I've already used the between function with two different dates, like this:

SELECT * FROM 'producao_hora' where data between '19/04/2018' and '20/04/2018'

But I'm not sure how to turn the datepicker date range into a variable that I could play in this select . How could it be done? Note: PHP codes are also welcome!

    
asked by anonymous 20.04.2018 / 14:59

1 answer

0

Good evening Renata,

How you are working with PHP. You can simply perform the form submit. And in the script that receives the request split the string to get the two dates. And then build your query.

form.html:

<form action="pesquisar.php" method="POST">
    <div class="row">
        <div class="col-md-12">
            <input type="text" name="daterange" value="01/01/2012 - 20/04/2018" />
            <button type="submit">Filtrar</button>
        </div>
    </div>
</form>
<script type="text/javascript">
    $(function() {
        $('input[name="daterange"]').daterangepicker({
            timePicker: false,
            timePickerIncrement: 0,
            locale: {
                format: 'DD/MM/YYYY'
            }
        });
    });
</script>

search.php:

<?php
    $datas = explode(' - ', $_POST['daterange']);
    echo $datas[0];
    echo "<br>";
    echo $datas[1];

See that I used the explode function to separate the dates from the range. So the new variable will have in the first position the starting date of the interval and in the second position the end of the interval.

    
20.04.2018 / 23:15