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
|