'How to read value from daterangepicker?
I have this daterangepicker runs on my local PHP site. Here is the code for HTML part
<form role="form" class="form-inline" method="post" action="">
<div id="reportrange" class="form-control">
<i class="fa fa-calendar"></i>
<span></span>
<b class="caret"></b>
</div>
<button class="btn btn-white" type="submit">Search</button>
</form>
Javascript part:
$('input[name="daterange"]').daterangepicker();
$('#reportrange span').html(moment().subtract(29, 'days').format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));
$('#reportrange').daterangepicker({
autoApply: true,
format: 'MM/DD/YYYY',
startDate: moment().subtract(29, 'days'),
endDate: moment(),
minDate: '01/01/2012',
maxDate: '12/31/2015',
dateLimit: { days: 60 },
showDropdowns: true,
showWeekNumbers: true,
timePicker: false,
timePickerIncrement: 1,
timePicker12Hour: true,
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
},
opens: 'right',
drops: 'down',
buttonClasses: ['btn', 'btn-sm'],
applyClass: 'btn-primary',
cancelClass: 'btn-default',
separator: ' to ',
locale: {
applyLabel: 'Submit',
cancelLabel: 'Cancel',
fromLabel: 'From',
toLabel: 'To',
customRangeLabel: 'Custom',
daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr','Sa'],
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
firstDay: 1
}
}, function(start, end, label) {
console.log(start.toISOString(), end.toISOString(), label);
$('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
});
I can select the date well, But no readable value on submit. Already follow this suggestion getting the value of daterangepicker but still no luck. Any helps is appreciated. Thanks.
Solution 1:[1]
Found the solution here easy-to-use-bootstrap-date-range-picker
Solution 2:[2]
The daterangepicker library will dynamically create an <input> tag with name="daterange". So, when you submit your form, the daterange form variable should contain the value you want. It will be formatted something like this:
01/15/2015 - 02/15/2015
If that is not a format that you want, you could create two hidden input fields (one for start date and one for end date) and set their values whenever your date range is changed.
<input type="hidden" name="start">
<input type="hidden" name="end">
Solution 3:[3]
$('#reportrange').data('daterangepicker').startDate;
$('#reportrange').data('daterangepicker').endDate;
This question solves it all.In my case the answer was not working because I was accessing the value before Declaring a date range picker. getting the value of daterangepicker bootstrap
Solution 4:[4]
You may use default php function explode() and it will return array with 2 elements
$daterange = $_POST['date_range'];
$split = explode('-', $daterange);
#check make sure have 2 elements in array
$count = count($split);
if($count <> 2){
#invalid data
}
$start = $split[0];
$end = $split[1];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Willy Mularto |
| Solution 2 | Andrew Mairose |
| Solution 3 | Community |
| Solution 4 | hannan yusop |
