# how do you get a list of all dates between two dates more than a day apart?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
array or all the date between and including those two dates.

I want to include this list in a query so I need it in a format like:
'2005-01-01', '2005-01-02',...

Any ideas? thanks

## Re: how do you get a list of all dates between two dates more than a day apart?

<?
\$start = "2005-05-05";
\$end = "2005-06-05";

\$init_date = strtotime(\$start);
\$dst_date = strtotime(\$end);

\$offset = \$dst_date-\$init_date;

\$dates = floor(\$offset/60/60/24) + 1;

for (\$i = 0; \$i < \$dates; \$i++)
{
\$newdate = date("Y-m-d", mktime(12,0,0,date("m", strtotime(\$start)),
(date("d", strtotime(\$start)) + \$i), date("Y", strtotime(\$start))));
echo \$newdate ."<br>";
}
?>

## Re: how do you get a list of all dates between two dates more than a day apart?

I'd have done something less mathematical.
Maybe something like this: (sorry it looks a lot, it isn;t really, I
just got overenthusiastic with comments and perfecting it!)

<?php

print_r(getAllDays("2005-01-01", "2005-01-24")); // the original
problem
echo "<br /><br />\n\n";
print_r(getAllDays("2005-01-11", "2005-03-24")); // going over month
boundaries
echo "<br /><br />\n\n";
print_r(getAllDays("2005-01-11", "2004-12-14")); // going backwards in
time
echo "<br /><br />\n\n";
print_r(getAllDays("2000-03-11", "2000-02-11")); // leap year? oh yes!
echo "<br /><br />\n\n";
print_r(getAllDays("2005-01-01", "2005-01-24", false)); // the original
problem returned as an array

function getAllDays(\$start, \$end, \$aslist = true) {
// convert the strings we get in to a timestamp
\$start  = strtotime(\$start);
\$end    = strtotime(\$end);

// this will make sure there isn't an infinite loop by deciding
//  which way (back or forwards one day) the loop should go
//  based on whether the start date is before the end date or not
\$whichway = (\$start < \$end) ? "tomorrow" : "yesterday";

// we'll increment \$curday so set it to the start date
\$curday = \$start;

// initialise the \$days array and add our first date to it (could
be done in one line but looks nicer like this)
\$days = array();
\$days[] = date("Y-m-d", \$curday);

// iterate through the days until we reach the end date
while (\$curday != \$end) {
// get the 'next' day in the sequence (might be forwards OR
backwards one day)
\$curday = strtotime(\$whichway, \$curday);
\$days[] = date("Y-m-d", \$curday);
}

// if we only wanted an array back, return the array now
if (\$aslist === false) return \$days;

// if we wanted a formatted list...

// inititalise empty string for the list
\$daylist = "";

// go through each date in the array
foreach (\$days as \$day) {
// add it to the string and stick a comma on the end
\$daylist .= \$day.", ";
}

// take the trailing comma-space off
\$daylist = substr(\$daylist, 0, -2);

return \$daylist;
}

?>

## Re: how do you get a list of all dates between two dates more than a day apart?

Notgiven wrote:

<?php
\$d0 = gmmktime(0, 0, 0, 1, 1, 2005);
\$d1 = gmmktime(0, 0, 0, 1, 24, 2005);
\$day = 24*60*60;

\$date_list = array();
for (\$date_index = \$d0; \$date_index <= \$d1; \$date_index += \$day) {
\$date_list[] = gmdate('Y-m-d', \$date_index);
}

print_r(\$date_list);
?>

--

## Re: how do you get a list of all dates between two dates more than a day apart?

Pedro Graca wrote:

Try this simple function:

<?php
\$date_array = date_range('10/20/2005','01/31/2006');
\$dates = "'" . implode("','",\$date_array) . "'";
echo \$dates . "<br />\n";

function date_range(\$sd,\$ed)
{
\$tmp = array()
\$sdu = strtotime(\$sd);
\$edu = strtotime(\$ed);
while (\$sdu <= \$edu) {
\$tmp[] = date('Y-m-d',\$sdu);
\$sdu = strtotime('+1 day',\$sdu);
}
}
?>

Ken

## Re: how do you get a list of all dates between two dates more than a day apart?

Pedro Graca wrote:

Try this simple function:

<?php
\$date_array = date_range('10/20/2005','01/31/2006');
\$dates = "'" . implode("','",\$date_array) . "'";
echo \$dates . "<br />\n";

function date_range(\$sd,\$ed)
{
\$tmp = array()
\$sdu = strtotime(\$sd);
\$edu = strtotime(\$ed);
while (\$sdu <= \$edu) {
\$tmp[] = date('Y-m-d',\$sdu);
\$sdu = strtotime('+1 day',\$sdu);
}
return (\$tmp);
}
?>

Ken

## Re: how do you get a list of all dates between two dates more than a day apart?

Ken Robinson wrote:

That was my simple function :) Mine just got out of control with

As for the sql BETWEEN operator, I was thinking that too... but I'm not
100% on sql so I was just kinda thinking "surely it has a between
operator".

## Re: how do you get a list of all dates between two dates more than a day apart?

Notgiven wrote:

Since you are planning to use the dates in a query (assuming you meant
a database query) there is no need to get a list of the dates.  Use the
BETWEEN operator in your WHERE clause.  For example to select all of the
dates in January 2005 you can use the following in your queries WHERE
clause:

WHERE data_date BETWEEN '2005-01-01' AND '2005-01-31'

HTH

Jerry

## Re: how do you get a list of all dates between two dates more than a day apart?

Wow - thanks so much to everyone who posted their ideas and functions!

Regarding the use of BETWEEN in sql code, I needed this function in case I
can't figure out a way to check for overlapping TIME and DATE interval in
the sql code.  If I can't, I would simply add records for every day of an
event instead of a start and end date.  That way, I would only check for
overlapping time within each day in the records.

Thanks again!