473,473 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
Jan 20 '06 #1
8 14949
How about something like this....

<?
$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>";
}
?>

Jan 20 '06 #2
Al
Adam Plocher wrote:
How about something like this....

<?
$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>";
}
?>


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;
}

?>

Jan 20 '06 #3
Notgiven wrote:
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',...


<?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);
?>

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Jan 21 '06 #4
Pedro Graca wrote:
Notgiven wrote:
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',...


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

Jan 21 '06 #5
Pedro Graca wrote:
Notgiven wrote:
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',...


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

Jan 21 '06 #6
Notgiven wrote:
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

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
Jan 21 '06 #7
Al

Ken Robinson 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


That was my simple function :) Mine just got out of control with
comments and stuff...

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".

Jan 21 '06 #8
"Notgiven" <no*********@invalid.invalid> wrote in message
news:wa***************@bignews7.bellsouth.net...
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


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!
Jan 23 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PW | last post by:
<rant> Sorry guys, but I just have to whinge. Dates in ASP are a total pain in the butt! I seem to get caught out so many times. I realise its my own fault, but going from the posts in this...
0
by: Tony | last post by:
Aloha, I have a problem that I can't think of a (easy) solution for. I'm building a web app (asp/access) that will allow people to book a cruise on a 24 passenger yacht that goes around the...
5
by: jnikle | last post by:
I have two completely unrelated tables, one for reviews and another for pay periods. The reviews table has a review date in it, and the pay periods table is just a list of the beginnings of pay...
18
by: PC Datasheet | last post by:
An Access user saw my name in a newsgroup and sent me a request for help on a project. As part of the project, a list of the dates in a month was needed. For anyone needing a list of dates in a...
1
by: sang | last post by:
Hi how to list the inserted dates by week,month and year. That is how to select the dates by past one week. similarly the month and year of the particular filed. Thanks in Advance Sang
2
by: Zyronne | last post by:
Hello Experts. I have a database that deals with history. Since MSSQL 2000 cannot accept dates earlier than Jan 1, 1753 I converted my column to CHAR. My problem now is I cannot find dates on...
4
by: Charlote | last post by:
Hello, I am a beginner in python, and have a question.. I need to create a list of all dates between two dates.. Lets say beggining date= 2008-1-15 enddate= 2008-2-20 how do I...
1
by: Matt Lowrance | last post by:
I have created an object that has a number of properties, that I am now trying to simply databind to a form. I am using the data designer to do it and just dragging and dropping the items onto the...
2
by: Luka Djigas | last post by:
Hello everyone, please, I need your help. I'm new to python, so I don't know if this will seem like a stupid question to some of you ... I have a need to write to a file (or just print on...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.