Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List all dates between a start and end date 1

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
0
0
SI
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

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;

}
 
Here's a nice short/simple solution:

Code:
<?
$start_date = '2005-06-01';
$end_date = '2006-06-16';

$date_array = DateArray($start_date,$end_date);
echo '<pre>';print_r($date_array);echo '</pre>';

function DateArray($s,$e)
{
$start = strtotime($s);
$end = strtotime($e);
$da = array();
for ($n=$start;$n <= $end;$n += 86400)
	$da[] = date('Y-m-d',$n);
return($da);
}
?>

Ken
 
Wow, many thanks, this indeed is a simpler solution that my function :)

Heh, I'm a self-taught programmer and while most of the time I get everything working I usually end up with a complicated function like the one above ;)

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top