Sha
Sha

Reputation: 506

How to get dates between two dates

List of Dates between my From and Two

<?php
$scheduleStartDate = 2015-06-20;
$scheduleEndDate = 2015-06-25;
$Date = getDatesFromRange($scheduleStartDate,$scheduleEndDate);
$Date = substr($Date, 0, -1);
function getDatesFromRange($start, $end){
    $dates = array($start);
    $Value = '';
    while(end($dates) < $end)
    {
        $dates[] = date('Y-m-d', strtotime(end($dates).' +1 day'));
        $Value .= '"'.date('j-n-Y', strtotime(end($dates).' +1 day')).'",';
    }
    return $Value;
}
?>

I'm passing it inside the script that I have , $Date is the one I got from above php file

<script>
        $( window ).load(function() {
        var availableDates = [<?php echo $Date?>];
        function available(date) {
          dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
          if ($.inArray(dmy, availableDates) != -1) {
            return [true, "","Available"];
          } else {
            return [false,"","unAvailable"];
          }
        }

        $('#date').datepicker({ 
            beforeShowDay: available,
            currentText: "Now",
            dateFormat: 'yy-mm-dd',
            inline: true,
            altField: '#datepicker_value', 
            onSelect: function(){
                    getData();
                    }   
        });
        });  
         </script>

Finally I'm enabling the dates in date picker But its enabling 2days late at stat and 1 day extra at the end
Say if I have dates from 20th to 25th it will enable dates from 22nd to 26th
Any help may greatly Appriciated.

Upvotes: 0

Views: 719

Answers (1)

Vlastislav Nov&#225;k
Vlastislav Nov&#225;k

Reputation: 349

Try this PHP code:

<?php
    $scheduleStartDate = '2015-06-20';
    $scheduleEndDate = '2015-06-25';
    $Date = getDatesFromRange($scheduleStartDate, $scheduleEndDate);
    $Date = substr($Date, 0, -1);
    function getDatesFromRange($start, $end){
        $startDate = new DateTime($start);
        $endDate = new DateTime($end);
        $endDate->modify('+1 day');
        $daterange = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
        $result = '';

        foreach($daterange as $date){
            $result .= '"'.$date->format("j-n-Y").'",';
        }
        return $result;
    }
?>

Upvotes: 1

Related Questions