Created
April 18, 2021 11:53
-
-
Save ManiruzzamanAkash/fb6f75421186ae1e823cce15e165602d to your computer and use it in GitHub Desktop.
Get dates of a day range from input using php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
Author : MD. Maniruzzaman Akash | |
Live Link: https://paiza.io/projects/e/TwNe185C_A2kJPZlXE6SGw?theme=twilight | |
**/ | |
$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 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment