473,795 Members | 3,457 Online
Bytes | Software Development & Data Engineering Community
+ 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 14978
How about something like this....

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

$init_date = strtotime($star t);
$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,d ate("m", strtotime($star t)),
(date("d", strtotime($star t)) + $i), date("Y", strtotime($star t))));
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($star t);
$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,d ate("m", strtotime($star t)),
(date("d", strtotime($star t)) + $i), date("Y", strtotime($star t))));
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 overenthusiasti c with comments and perfecting it!)

<?php

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

function getAllDays($sta rt, $end, $aslist = true) {
// convert the strings we get in to a timestamp
$start = strtotime($star t);
$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($whic hway, $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_l ist);
?>

--
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*********@in valid.invalid> wrote in message
news:wa******** *******@bignews 7.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
2144
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 newsgroup and others, I'm not the only one. Its just a poorly addressed issue within ASP. So for all your poor buggers out there that are having problems, particularly with european date formats, here is my solution. I have the user enter their...
0
1599
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 island. I have a table that holds the records when someone books a spot on the cruise. I have a query that counts how many bookings there are for a particular day subtracted from 24 so that I can see how many spots are open for that day. I also have...
5
3466
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 periods. What I need to do is take a given review date and find out which date in my pay periods table it's closest to, above or below. Any help is greatly appreciated. -Josh
18
6644
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 month, here is what I used: 1. Create a table named TblNumbers with one field named Num and populate the table with 1 to 31 2. Create a query based on TblNumbers 3. Pull down Num into the first field of the query. 4. Put the following...
1
4858
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
2335
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 a Range..If supposing my datatype is DATETIME, I would use DATE1 BETWEEN STARTDATE AND ENDDATE. Thank you in advance.
4
35802
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 get a list dates = ?
1
1383
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 form. Everything works great, except one of the items in my object is a list of dates. Right now I am using a generic list to hold these, but when I go to put this item on the form it only lists "None" as the type of control I can use to...
2
2253
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 screen, that part doesn't matter at this point) a list of dates, starting today. For example: 02.09.2008. tue 03.09.2008. wed et cetera
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.