简体   繁体   中英

How to fix custom date range issue in daterangepicker?

if($selected_date_interval == 'Yesterday'){
 $_SESSION['start_date_by_interval'] = "moment().subtract(1, 'days')";
 $_SESSION['end_date_by_interval'] = "moment().subtract(1, 'days')";
}
elseif ($selected_date_interval == 'Today'){
 $_SESSION['start_date_by_interval'] = "moment()";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif($selected_date_interval == 'Last 7 Days'){
 $_SESSION['start_date_by_interval'] = "moment().subtract(6, 'days')";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif ($selected_date_interval == 'Last 30 Days')
{
 $_SESSION['start_date_by_interval'] = "moment().subtract(30, 'days')";
 $_SESSION['end_date_by_interval'] = "moment()";
 }
elseif ($selected_date_interval == 'This Month'){
 $_SESSION['start_date_by_interval'] = "moment().startOf('month')";
 $_SESSION['end_date_by_interval'] = "moment().endOf('month')";
 }
elseif ($selected_date_interval == 'Last Month'){
$_SESSION['start_date_by_interval'] = "moment().subtract(1, 'month').startOf('month')";
 $_SESSION['end_date_by_interval'] = "moment().subtract(1, 'month').endOf('month')";
 }else{
  $_SESSION['start_date_by_interval'] = "moment($start_date,'MMMM D, YYYY')";
  $_SESSION['end_date_by_interval'] = "moment($end_date,'MMMM D, YYYY')";
  }

All type of my date ranges is working well instead of custom date. Whenever I select a custom date then the date is showing not correct. So for the custom date, I am passing the date type and format above in last else condition. What I am doing wrong please tell me.

Note- I am using http://daterangepicker.com library.

This is my datepicker javascript code.

$('#reportrange').daterangepicker({
            "showDropdowns": true,
            "autoApply": false,

            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(30, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
//                'Custom': [moment(end,'YYYY-MM-DD'), moment(start,'YYYY-MM-DD')]
//                'Custom Range': [end, start]
            },
            startDate: start,
            endDate: end,
            "locale": {
                "format": "YYYY-MM-DD",
                "separator": "-",
                "applyLabel": "Apply",
                "cancelLabel": "Cancel",
                "fromLabel": "From",
                "toLabel": "To",
                "customRangeLabel": "Custom",
                "weekLabel": "W",
                "daysOfWeek": [
                    "Su",
                    "Mo",
                    "Tu",
                    "We",
                    "Th",
                    "Fr",
                    "Sa"
                ],
                "monthNames": [
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                ],
                "firstDay": 1
            },
            opens: 'left'
        }, cb);

        cb(start, end);

    });

I am not sure if you got it working or not but I was also searching for solution of same issue and got fix for this.

Solution is as follows:

In your code you are defining : 'Custom Range': [end, start] instead of this you have to provide your start and end date in [moment().subtract(30, 'days'), moment()] format. So u will have to find difference of days and provide it in place of 'moment().subtract(30, 'days')' . For me this solution works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM