Print out all dates in a range?

This is a discussion on Print out all dates in a range? within the PHP General forums, part of the PHP Programming Forums category; (This is a duplicate from php.dev) Hi I need a way to get all of the date stamps between ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-06-2007
Alex
 
Posts: n/a
Default Print out all dates in a range?

(This is a duplicate from php.dev)

Hi

I need a way to get all of the date stamps between two date stamps.

For example, I might have 2007-02-05 and 2007-05-15, which is Feb.
2nd, 2007 through May 15, 2007.

What I want to do is just print out all of the stamps, in that format,
between those two dates.

For example:

2007-02-05
2007-02-06
2007-02-07
..........
2007-02-28
2007-03-01
..........
2007-05-15

I'd need the script to know if it's a leap year, etc...

I'm sure there is a way to do this, I just don't know of one.

Any help is really appreciated.

Alex

Reply With Quote
  #2 (permalink)  
Old 02-06-2007
Ken Robinson
 
Posts: n/a
Default Re: Print out all dates in a range?

"Alex" <cook@propellingsolutions.com> wrote in
news:1170721237.966107.291120@v45g2000cwv.googlegr oups.com:

> (This is a duplicate from php.dev)
>
> Hi
>
> I need a way to get all of the date stamps between two date stamps.
>
> For example, I might have 2007-02-05 and 2007-05-15, which is Feb.
> 2nd, 2007 through May 15, 2007.
>
> What I want to do is just print out all of the stamps, in that format,
> between those two dates.
>
> For example:
>
> 2007-02-05
> 2007-02-06
> 2007-02-07


I would use get the Unix timestamp of the starting date. Use that as the
start of a loop, adding 86400 (the number of seconds in a day) to the
loop index, and end when the loop index exceeds the end day. Either
print the dates from the loop or store them in an array.

Here is one solution:

<?php
function list_dates($start,$end) {
$ret = array();
$sts = strtotime($start);
$ets = strtotime($end);
if ($ets < $sts) return (false);
for($i=$sts;$i<=$ets;$i+=86400)
$ret[] = $i;
return($ret);
}

$dates = list_dates('December 29, 2007','March 4,2008');
if (is_array($dates))
foreach ($dates as $dt)
echo date('Y-m-d',$dt) . '<br>';
?>

Ken
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:57 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0