2

I am trying to Write Program for Calculating Next 20 dates After Specifying Start date, then from 20 dates i have Exclude Weekends & Holidays(Array holidays('2016-12-13',2016-12-24)) And Result Array which includes only Working Days Excluding Saturday & Sunday, from this Result Array after Passing Holiday array(Eg:- holidays('2016-12-13',2016-12-24))), it must be Excluded from result array. i:e;

I want Expected Output Below mentioned

.

    <?php 
$Date=array('2016-12-01');
echo "\n <br />Start Date:-" . $Date[0] . ""; 

                            /*Code For Generating Next 20 Dates Starts*/
//$start = strtotime($s_row['schedule_start_date']);

$start = strtotime('2016-12-01');
$dates=array();
for($i = 0; $i<20; $i++)
{
    array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
echo "\n <br /> Array Of next 20 Days/dates of Given:-";
print_r($dates);

$start=array();
$start=$dates;                            /*Code For Generating Next 20 Dates Ends*/
$result=array();

$start = strtotime(array_values($Date)[0]);
//$end = strtotime(array_values($Date)[30]);
$result = array();
$begin = new DateTime( '2016-12-01' );
$end = new DateTime( '' );
//$end = $end->modify( '+1 day' ); 

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date)
{
   //echo $date->format("Y-m-d") . "<br>";
   if (date('N', $start) <= 5)     /* 'N' number days 1 (mon) to 7 (sun) */
                                    /*5 weekday */   
   {
        $current = date('Y-m-d', $start);     //m/d/Y
        $result[$current] = '';

    }
    $start += 86400;
    //echo "Days Without Sat Sun".$result[date($date->format("Y-m-d"))];
      //echo "Days Without Sat Sun".$result2[date($current->format("Y-m-d"))];  

} 
echo " \n <br /> Dates Without Weekends LIKE (Excluding Saturday & Sunday):-";
print_r($result);



        /*For Holiday*/
$FinalArray = array();        
$holidays = array(
  '2016-12-13',
  '2016-12-24',
);
echo " \n <br /> Given Holiday Dates Are:-";
print_r($holidays);



$a1 = $result;
 $a2 = $holidays;
 $array =  array_diff(array_merge($a1,$a2),array_intersect($a1,$a2));

echo "\n <br /> Output:-";
 print_r($array);

?>

it Gives Output as :- Array ( [2016-12-01] => [2016-12-02] => [2016-12-05] => [2016-12-06] => [2016-12-07] => [2016-12-08] => [2016-12-09] => [2016-12-12] => [2016-12-13] => [2016-12-14] => [2016-12-15] => [2016-12-16] => [2016-12-19] => [2016-12-20] => [2016-12-21] => [2016-12-22] => [2016-12-23] => [0] => 2016-12-13 [1] => 2016-12-24 )

> But I Want Expected Output:-

 Array ( [2016-12-01] => [2016-12-02] => [2016-12-05] => [2016-12-06] => [2016-12-07] => [2016-12-08] => [2016-12-09] => [2016-12-12] => [2016-12-14] => [2016-12-15] => [2016-12-16] => [2016-12-19] => [2016-12-20] => [2016-12-21] => [2016-12-22] => [2016-12-23]

You Can Notice That 2016-12-13 is Not There in Above Expected Output as in '2016-12-13', 2016-12-24 is passed as Holiday via holiday array ($holidays = array( '2016-12-13', '2016-12-24', );) i:e; if i pass any date through holidays array it should not be included in result Array(). i:e 2016-12-13 is Available in Result array as well as holiday array So While while printing Final OUTPUT:- 13th date(2016-12-13) Should not be Included in final Output. Anybody Solve this will be Appreciated Thanks in Advance.

3
  • 1
    in-spite of using built-in php array functions, Solution in any other way using foreach loop or any function will appreciated Commented Dec 24, 2016 at 4:46
  • array_unique() function may help you Commented Dec 24, 2016 at 4:48
  • Related: stackoverflow.com/q/10595524/2943403 Commented Sep 18, 2022 at 10:01

3 Answers 3

1

When I have to remove duplicates from a array the function that I keep going back to is

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

you can find the documentation Here

<?php
  $input = array("a" => "green", "red", "b" => "green", "blue", "red");
  $result = array_unique($input);
  print_r($result);
?>

the output

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

I hope that this was able to help

Sign up to request clarification or add additional context in comments.

Comments

0

I prefer to calculate all dates just in one pass. (You may skip filling $dates and $dates_mon_fri arrays if they doesn't used in output also.) There is yet another approach to avoid array_diff() and array_unique() functions. I've used an array_flip() to exchange keys with values in $holdidays array to use fast array_key_exists() function.

<?php

$start = strtotime('2016-12-01');

$holidays = [
  '2016-12-13',
  '2016-12-24',
];

$dates = [];
$dates_mon_fri = [];
$dates_working = [];
$flip_holidays = array_flip($holidays);

for ($i = 0; $i < 20; $i++) {
    $timestamp = strtotime("+$i day", $start);
    $date = date('Y-m-d', $timestamp);

    $dates[] = $date;

    $mon_fri = false;
    if (date('N', $timestamp) <= 5) {
        $dates_mon_fri[] = $date;
        $mon_fri = true;
    }

    if ($mon_fri && !array_key_exists($date, $flip_holidays)) {
        $dates_working[] = $date;
    }
}

var_dump($dates);
var_dump($dates_mon_fri);
var_dump($dates_working);

Comments

0

You can avoid using explicit looping:

$begin = new DateTimeImmutable('2016-12-01');
$end = $begin->modify('+20 days');

$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);

$allDates = iterator_to_array($daterange);
$datesExcludingWeekends = array_filter($allDates, function ($date) {
     return (int) $date->format("N") < 6;
});
$datesExcludingWeekends = array_map(
    'date_format',
    $datesExcludingWeekends,
    array_fill(1, count($datesExcludingWeekends), 'Y-m-d')
);

$holidays = [
  '2016-12-13',
  '2016-12-24',
];

$datesExcludingWeekendsIncludingHolidays = array_flip(array_merge(
    $datesExcludingWeekends,
    array_diff($holidays, $datesExcludingWeekends)
));

Here is working demo.

Also, take a look at the Carbon library. If you need some exhaustive working with dates this library can really ease your life.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.