Created
September 5, 2022 15:52
-
-
Save ahmu83/d7d3afa6a2162155a9b182b814c27a8c to your computer and use it in GitHub Desktop.
Get next n number of dates from a specified date or the current date
This file contains hidden or 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 | |
/** | |
* Get next n number of dates from a specified date | |
* | |
* @param Integer $n Number of dates | |
* @param String $from Now or the from date | |
* @param Boolean $exclude_weekends Exclude weekends | |
* @return Array | |
*/ | |
function get_next_dates($n = 5, $from = 'now', $exclude_weekends = true) { | |
$date = ( $from == 'now' ? date('Y-m-d') : date('Y-m-d', strtotime($from)) ); | |
$dates = array(); | |
for ( $i = 0; $i < $n; $i++ ) { | |
$d = date('Y-m-d', strtotime($date . ' +' . $i . ' day')); | |
$day = date('D', strtotime($date . ' +' . $i . ' day')); | |
if ( $exclude_weekends && in_array($day, array('Sat', 'Sun')) ) { | |
$n = $n + 1; | |
continue; | |
} | |
$dates[] = $d; | |
} | |
return $dates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment