k somro
k somro

Reputation: 1

Find Number of days in seasons range by providing from and to date

Have implemented code for single season where date range doesn't overlap and it works fine

Online PHP Code : https://onlinephp.io/c/1ca08

$ranges = array(
    'low' => array(
                    array( 'start' => "15 March 2023", 'end' => "15 May 2023") // 15 March - 15 May
                ),
    'high' => array(
                    array( 'start' => "16 May 2023", 'end' => "30 June 2023"), // 16 May - 16 June
                    array( 'start' => "1 July 2023", 'end' => "14 March 2024")
                ),
    'winter' => array(
                    array( 'start' => "1 December 2023", 'end' => "15 March 2023")
                ),
    'summer' => array(
                    array( 'start' => "1 June 2023", 'end' => "30 August 2023")
                    )
            );
             
             
// High Season Inputs 
$start_date = '2023-05-17'; // 17 May 2023
$end_date =   '2023-05-25'; // 25 May 2023
// ----- OUTPUT - Working -----------------------------------
// [high] Season and no.of nights: 8

// Low Season Inputs 
// $start_date = '2023-03-16'; // 16 March 2023
// $end_date =   '2023-03-21'; // 21 March 2023
// ----- OUTPUT - Working -----------------------------------
// [low] Season and no.of nights: 5

// Summer Season Inputs 
// $start_date = '2023-06-01'; // 01 June 2023
// $end_date =   '2023-06-10'; // 10 June 2023
// ----- OUTPUT - Working -----------------------------------
// [summer] Season and no.of nights: 5

// Overlap season Low to High - 
// $start_date = '2023-05-12'; // 12 May 2023
// $end_date =   '2023-05-18'; // 18 May 2023
// ----- OUTPUT -----------------------------------
// [low] Season and no.of nights: 4
// [high] Season and no.of nights: 2

// Overlap season High to Low - 4 in high + 5 in low
// $start_date = '2023-03-11'; // 11 March 2023
// $end_date =   '2023-03-20'; // 20 March 2023
// ----- OUTPUT -----------------------------------
// [low] Season and no.of nights: 4
// [high] Season and no.of nights: 5

// Overlap multiple season 
// $start_date = '2023-05-12'; // 12 May 2023
// $end_date =   '2023-06-10'; // 10 June 2023
// ----- OUTPUT -----------------------------------
// [low] Season and no.of nights: 4
// [high] Season and no.of nights: 26
// [summer] Season and no.of nights: 9

$i = 1;
foreach($ranges as $key => $single_range) {
    
    foreach($single_range as $range) {
        $date1 = new DateTime($start_date);
        $date2 = new DateTime($end_date);
        $totalnumberOfNights= $date2->diff($date1)->format("%a");
        
        $date_range_start   =  new DateTime($range['start']);
        $date_range_end     =  new DateTime($range['end']);

        
        if ( ( ( (strtotime( $start_date ) >= strtotime( $range['start'] ) ) && ( strtotime( $end_date ) <= strtotime( $range['end'] ) ) ) ) ) {
            $no_of_season_night = $date2->diff($date1)->format("%a");
            
            echo "$i-) The selected dates exists in [$key] Season and no.of nights in this range: [$no_of_season_night] = $totalnumberOfNights \n";

        }
  
    }
     $i++;
} 

Have implemented code for single season where date range doesn't overlap and it works fine.

I need someone to help me find multiple season nights in a specific date range. LIKE if user input are

Upvotes: 0

Views: 44

Answers (0)

Related Questions