Days intervals in PHP
I needed a funtion that provided me with an array of all the dates between two given dates. This is quite a trivial function, but all over the net there are several implementations which are often quite bloated. By leveraging the power of strtotime it’s actually pretty easy!
function getDays($start, $end, $format) {
// adding the first day to the interval
$days[] = date($format, strtotime($start));
$current = $start;
// While the current date is less than the end date
while (strtotime($current) < strtotime($end)) {
$current = date($format, strtotime("+1 day", strtotime($current)));
$days[] = $current;
}
return $days;
}
strtotime converts pretty much any date format you throw at it to UNIX time, allows you to do arithmetic with days (hence the +1 day), where date reformats it again :)