Skip to content

Instantly share code, notes, and snippets.

@ahmu83
Created September 5, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmu83/d7d3afa6a2162155a9b182b814c27a8c to your computer and use it in GitHub Desktop.
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
<?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