Algorithm


Code Examples

#1 Code Example with PHP Programming

Code - PHP Programming


<?php
$days     = ['Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

$from_day = 'Saturday'; // From day input from frontend Input
$to_day   = 'Monday';  // To day input from frontend Input

$from_day_index = array_search( $from_day, array_values( $days ) );
$to_day_index   = array_search( $to_day, array_values( $days ) );

/* Get days between this range */
$days_between_range = [];
foreach ( $days as $key => $day ) {
    if( $key >= $from_day_index && $key <= $to_day_index ) {
        $days_between_range[] = $day; 
    }
}

$date     = date('y-m-d'); // FROM DATE =>> get current date
$end_data = '2021-05-01'; // Get from Frontend Input

$dates = [];

while(strtotime($date) < strtotime( $end_data ) ) {
	
	$day_num  = date( 'd', strtotime( $date ) );
	$day_name = date( 'l', strtotime( $date ) );
	$date    = date("Y-m-d", strtotime("+1 day", strtotime($date)));
	
	if ( in_array( $day_name, $days_between_range ) ) {
	    $dates[] = $date;
	}
}
    
print_r($dates);

/************************ 
Expected output: 
*************************
Array
(
    [0] => 2021-04-19
    [1] => 2021-04-20
    [2] => 2021-04-25
    [3] => 2021-04-26
    [4] => 2021-04-27
)
*/
Copy The Code & Try With Live Editor

Output

x
+
cmd
Array
(
[0] => 2021-04-19
[1] => 2021-04-20
[2] => 2021-04-25
[3] => 2021-04-26
[4] => 2021-04-27
)
Advertisements

Demonstration


Get dates of a given Day in PHP and MySQL

Previous
Write a PHP Code to find the numbers with less occurance start to finish with number of occurance