-2

I am developing a dynamic website where the website backend admin will select two dates (From - To) and the user in the front-end will be able to get a dropdown list between those two dates excluding weekends along with the name of the day (For e.g: <option>27th April 2015 - Monday</option>)

I tried going by this link but in vain: Link to get date range between two dates excluding weekends.

Please Help.

The table row name for first date is skype_date_start and for the second date is skype_date_end.

My code as follows:

`$start = '2013/01/01'; //start date
$end = '2013/01/30'; //end date

$dates = array();
$start = $current = strtotime($start);
$end = strtotime($end);

while ($current <= $end) {
    $dates[] = date('Y/m/d', $current);
    $current = strtotime('+1 days', $current);
}

//now $dates hold an array of all the dates within that date range
print_r($dates);`

This only prints the array in a cluster form like : Array ( [0] => 2013/01/01 [1] => 2013/01/02 [2] => 2013/01/03 [3] => 2013/01/04 [4] => 2013/01/05 [5] => 2013/01/06 [6] => 2013/01/07 [7] => 2013/01/08 [8] => 2013/01/09 [9] => 2013/01/10 [10] => 2013/01/11 [11] => 2013/01/12 [12] => 2013/01/13 [13] => 2013/01/14 [14] => 2013/01/15 [15] => 2013/01/16 [16] => 2013/01/17 [17] => 2013/01/18 [18] => 2013/01/19 [19] => 2013/01/20 [20] => 2013/01/21 [21] => 2013/01/22 [22] => 2013/01/23 [23] => 2013/01/24 [24] => 2013/01/25 [25] => 2013/01/26 [26] => 2013/01/27 [27] => 2013/01/28 [28] => 2013/01/29 [29] => 2013/01/30 ).

I cannot exclude the weekends and also cannot get it to be printed as <option>27th April, 2015 - Monday</option><option>28th April 2015 - Tuesday</option> & so on... to the last date.

Community
  • 1
  • 1
Arijit Aich
  • 161
  • 1
  • 2
  • 12
  • I Guess This Question Is Too Tough To Be Answered !! – Arijit Aich Apr 07 '15 at 21:23
  • Well, for a start, there's no actual question here. For another, there's also no code. Could you add your current code to the question? What is the problem you're having with it? Why doesn't it work? What doesn't it do that it's supposed to? – andrewsi Apr 08 '15 at 02:47

2 Answers2

0

For the Date Formatting use this:

$dates[] = date('j F, Y - D', $current);

instead of

$dates[] = date('Y/m/d', $current);

for skipping weekends: (Ref: https://stackoverflow.com/a/4802362/1741052)

use a function to determine the current date if its a weekend or not

function isWeekend($date) {
$weekDay = date('w', strtotime($date));
return ($weekDay == 0 || $weekDay == 6);
}

Try the below.. Code Example:

$start = '2013/01/01'; //start date
$end = '2013/01/31'; //end date

$dates = array();
$start = $current = strtotime($start);
$end = strtotime($end);

while ($current <= $end) 
{
    $weekDay = date('w', $current); 
    print(" <br/> Week Day: {$weekDay}");
    if($weekDay != 0 && $weekDay != 6)
    {
        // Not a Weekend
        $dates[] = date('j F, Y - D', $current);    
        $current = strtotime('+1 days', $current);
    }
}

//now $dates hold an array of all the dates within that date range
print_r($dates);
Community
  • 1
  • 1
Rohit Batra
  • 674
  • 4
  • 18
0

You are almost there. Before adding a date to dates array you need to check if its a week day or not. If it is then add it otherwise skip that date and continue to next iteration. Like this:

<?php
    $start = '2013/01/01'; //start date
    $end = '2013/01/30'; //end date

    $currentDate = strtotime($start);
    $endDate = strtotime($end);
    $dates = array();

    while ($currentDate <= $endDate ) {
        if(!isWeekend($currentDate)){
            $dates[] = date('Y/m/d', $currentDate );
        }

        $currentDate = strtotime('+1 days', $currentDate );
    }

    function isWeekend($date) {
        return date('w', $date) == 0 || date('w', $date) == 6;
    }
?>

Next you need to create your select tag like this:

<select name=date_range=>
    <?php
         for($i = 0; $i < count($dates); $i++) {
              echo "<option value='{$dates[$i]}'>";
              echo date("Y/m/d - l", strtotime($dates[$i]));
              echo "</option>";
         }
    ?>
</select>
Shoaib Shakeel
  • 1,447
  • 18
  • 24
  • i am a very new comer in php. Can you do me a favor to write down the total complete code including the weekend functions ? It would be of great help. – Arijit Aich Apr 08 '15 at 08:03
  • `weekend` function is already there. Look at my answer. Also i've provided you with code for building `select` element too. So what more do you need. – Shoaib Shakeel Apr 08 '15 at 10:01
  • It says the follow - `Missing argument 1 for isWeekend()` – Arijit Aich Apr 08 '15 at 10:39
  • That's what it is showing: ` ` I have deleted some options as it was not fitting in here. But all the options were same. – Arijit Aich Apr 08 '15 at 10:52
  • i forgot to convert data to timestamp before outputting in `option` tag. It's fixed now. – Shoaib Shakeel Apr 08 '15 at 11:00
  • Also, the dates in the option values which seem to work are not filtering the weekends. – Arijit Aich Apr 08 '15 at 11:00
  • This is fetching the dates with the days as expected but it is not skipping the weekends as expected. – Arijit Aich Apr 08 '15 at 11:07