Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

how do you get a list of all dates between two dates more ..

 
   Database Forums (Home) -> PHP RSS
Next:  Fulltext on ntext column  
Author Message
Notgiven

External


Since: Jan 06, 2006
Posts: 7



(Msg. 1) Posted: Fri Jan 20, 2006 4:03 pm
Post subject: how do you get a list of all dates between two dates more th
Archived from groups: comp>lang>php (more info?)

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

 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Adam Plocher

External


Since: Jan 17, 2006
Posts: 7



(Msg. 2) Posted: Fri Jan 20, 2006 4:03 pm
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Al

External


Since: Jan 12, 2006
Posts: 3



(Msg. 3) Posted: Fri Jan 20, 2006 4:03 pm
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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

?>
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Ken Robinson

External


Since: Sep 08, 2005
Posts: 3



(Msg. 4) Posted: Fri Jan 20, 2006 6:28 pm
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Ken Robinson

External


Since: Sep 08, 2005
Posts: 3



(Msg. 5) Posted: Fri Jan 20, 2006 6:28 pm
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Jerry Gitomer

External


Since: Feb 05, 2005
Posts: 13



(Msg. 6) Posted: Fri Jan 20, 2006 11:55 pm
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Al

External


Since: Jan 12, 2006
Posts: 3



(Msg. 7) Posted: Sat Jan 21, 2006 10:23 am
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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 Smile 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".
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Notgiven

External


Since: Jan 06, 2006
Posts: 7



(Msg. 8) Posted: Mon Jan 23, 2006 7:34 am
Post subject: Re: how do you get a list of all dates between two dates mor [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Notgiven" wrote in message

> 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!
 >> Stay informed about: how do you get a list of all dates between two dates more .. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Dates before 1969 - I have written a program that uses the built-in PHP date functions. All that the program does is calculate the amout of time between two dates, or calculate the date a certain amount of time before or after another date. It works, but I didn't realize..

still having problems with dates - Hi I am niew to pho and am having trouble with dates. I can format a date ok so $today=date('d M Y' ) gives me 2 Nov 2005 $dateval is a mysql date yyyy-mm-dd hh:nn:sss (2005-10-26 13:32:09) $today=date('d M Y' ,$dateval) gives me something like 1 jul...

How to reformat dates? - PROBLEM #1: I'm trying to convert an old database that has a date field (as a string) that has a variety of different formats. I'd like to standardize what is already in the database so that I can create an input form using dropdown menus that will..

Diff Between 2 Dates - I am trying to find the difference between 2 dates. The problem that I am having is that all routines in the PHP Manual and all of the scripts I have seen on the Internet only allow a date as old as 1970. I am trying to find the age of a person in..

creating dates from 3-tuples - Given a 3-tuple of some day, month, and year that checkdate would return true for, how would I go about converting that day, month, and year to something like September 3, 1997 using gmdate? mktime(0,0,0,$month,$day,$year) doesn't seem to work as often a...
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]