× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to get an array of all dates between two dates in PHP

<p>If you have ever needed to quickly generate an array or "list" of all dates between two specific dates in time, you can use this script to do so.</p> <p> </p> <pre>/** * Returns every date between two dates as an array * @param string $startDate the start of the date range * @param string $endDate the end of the date range * @param string $format DateTime format, default is Y-m-d * @return array returns every date between $startDate and $endDate, formatted as "Y-m-d" */ function createDateRange($startDate, $endDate, $format = "Y-m-d") { $begin = new DateTime($startDate); $end = new DateTime($endDate); $interval = new DateInterval('P1D'); // 1 Day $dateRange = new DatePeriod($begin, $interval, $end); $range = []; foreach ($dateRange as $date) { $range[] = $date-&gt;format($format); } return $range; }</pre> <p> </p> <p>This will return an array of all dates between two dates, you use the script like this:</p> <p> </p> <pre>// Return all dates between 2015-01-01 to 2015-02-05 as an array createDateRange("2015-01-01", "2015-02-05");</pre> <p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus