Skip to content

Instantly share code, notes, and snippets.

@aliuygur
Created December 23, 2014 12:37
Show Gist options
  • Save aliuygur/185b1c7b20d19774c551 to your computer and use it in GitHub Desktop.
Save aliuygur/185b1c7b20d19774c551 to your computer and use it in GitHub Desktop.
<?php
/**
* Creating date collection between two dates
*
* <code>
* <?php
* # Example 1
* date_range("2014-01-01", "2014-01-20", "+1 day", "m/d/Y");
*
* # Example 2. you can use even time
* date_range("01:00:00", "23:00:00", "+1 hour", "H:i:s");
* </code>
*
* @author Ali OYGUR <alioygur@gmail.com>
* @param string since any date, time or datetime format
* @param string until any date, time or datetime format
* @param string step
* @param string date of output format
* @return array
*/
function date_range($first, $last, $step = '+1 day', $output_format = 'd/m/Y' ) {
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment