Make datepicker date range to be used in mysql

Asked

Viewed 416 times

0

In my code, I have a calendar where the user selects the period 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 used the function betweenwith two distinct dates, thus:

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

But I don’t know how to turn the datepicker date range into a variable I could play in that select. How could it be done? NOTE: Codes in PHP are also welcome!

1 answer

0

Good night Renata,

As you are working with PHP. You can simply perform the form Ubmit. And in the script that receives the request split the string to get the two dates. And later mount your query.

html form.:

<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];

Note that I used the function explodes to separate the dates of the interval. So the new variable will have at first position the initial date of the interval and at second position the end of the interval.

Browser other questions tagged

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