note 82515 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 03 Aug 2008 11:54:34 +0000 |
| Subject: | note 82515 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143112@lists.php.net to get a copy of this message | ||
Note Submitter: brian at diamondsea dot com
----
I am having problems with the timhavens's <i>dates_betweeen_dates()</i> script not
returning proper output for calls like:
<?php
print_r(dates_between_dates('2008-04-14', '2008-04-14'));
print_r(dates_between_dates('2008-04-14', '2008-04-15'));
?>
Here is a function that returns the expected results:
<?php
/*
* dates_between_dates - Returns array of all dates between $date1 and $date2
*
* @param start_date Start-Date of range
* @param end_date End-Date of range
* @returns array dates
*/
function dates_between_dates($start_date, $end_date) {
// Standardize date formats
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = date('Y-m-d', strtotime($end_date));
// if $end_date is later, swap the dates
if (strtotime($start_date) > strtotime($end_date)) {
$a = $start_date;
$start_date = $end_date;
$end_date = $a;
}
// initialize array with start date
$return_array = array($start_date);
// Initialize loop counter date
$the_date = $start_date;
// loop through days
while ($the_date != $end_date) {
$the_date = date('Y-m-d', strtotime($the_date."+1 day"));
$return_array[] = $the_date;
}
return $return_array;
}
?>