Hello all,
I've decided to post this here as I always get great suggestions here
So, I need a function that should return an array with all dates (Y-m-d) between two dates I input.
For example if I input start at '2005-06-01' and end at '2005-06-16' it should return this array:
Array
(
[0] => 2005-06-01
[1] => 2005-06-02
[2] => 2005-06-03
[3] => 2005-06-04
[4] => 2005-06-05
[5] => 2005-06-06
[6] => 2005-06-07
[7] => 2005-06-08
[8] => 2005-06-09
[9] => 2005-06-10
[10] => 2005-06-11
[11] => 2005-06-12
[12] => 2005-06-13
[13] => 2005-06-14
[14] => 2005-06-15
[15] => 2005-06-16
)
The dates will allways be in the 'Y-m-d' format and it can be any two dates.
I did write a function that does this (see below), but as usually I am hoping to learn something from any suggestions/more efficient examples you can provide. So, any suggestions on how to improove my function below?
Many thanks!
c4n
I've decided to post this here as I always get great suggestions here
So, I need a function that should return an array with all dates (Y-m-d) between two dates I input.
For example if I input start at '2005-06-01' and end at '2005-06-16' it should return this array:
Array
(
[0] => 2005-06-01
[1] => 2005-06-02
[2] => 2005-06-03
[3] => 2005-06-04
[4] => 2005-06-05
[5] => 2005-06-06
[6] => 2005-06-07
[7] => 2005-06-08
[8] => 2005-06-09
[9] => 2005-06-10
[10] => 2005-06-11
[11] => 2005-06-12
[12] => 2005-06-13
[13] => 2005-06-14
[14] => 2005-06-15
[15] => 2005-06-16
)
The dates will allways be in the 'Y-m-d' format and it can be any two dates.
I did write a function that does this (see below), but as usually I am hoping to learn something from any suggestions/more efficient examples you can provide. So, any suggestions on how to improove my function below?
Many thanks!
c4n
Code:
$start='2005-06-01';
$end='2005-06-16';
$mylist=DateArray($start,$end);
print_r($mylist);
function DateArray($start,$stop) {
$myDateArray=array();
list($start_year,$start_month,$start_day)=explode('-',$start);
list($end_year,$end_month,$end_day)=explode('-',$stop);
for ($year=$start_year; $year<=$end_year; $year++) {
if ($year==$start_year) {
/* Start with start month */
$start_months_at=$start_month;
} else {
/* Start at first day of the month */
$start_months_at=1;
}
if ($year==$end_year) {
/* End months at the end_month */
$end_months_at=$end_month;
} else {
/* End months at the last month */
$end_months_at=12;
}
for ($month=$start_months_at; $month<= $end_months_at; $month++) {
if ($year==$start_year && $month == $start_month) {
/* Start days at the start day */
$start_days_at=$start_day;
} else {
/* Start at first day of the month */
$start_days_at=1;
}
if ($year==$end_year && $month == $end_month) {
/* Stop days at the end day */
$stop_days_at=$end_day;
} else {
/* Stop days at the end of month */
$stop_days_at=date("t", mktime(0,0,0,$month,1,$year));
}
for ($day=$start_days_at; $day<=$stop_days_at; $day++) {
$myDateArray[]=sprintf("%04d-%02d-%02d",$year,$month,$day);
}
}
}
return $myDateArray;
}