[RESOLVED] How can I find every day in a date span?
Hello.
I have a date span, like 90 days ago till today, is 2006-11-12 to 2007-02-10, and I need to know every date in the span, like below:
2006-11-12
2006-11-13
2006-11-14
2006-11-15
2006-11-16
etc.
any help would be appreciated much, thanks
PHP Code:
<?php
$fr_yr = 2006 ;
$fr_mo = 11 ;
$fr_dy = 12 ;
$date_span = 90 ;
$arrDATES = array();
FOR ( $x = 0 ; $x < $date_span ; $x ++) :
$arrDATE [] = date ( "Y-m-d" , mktime ( 0 , 0 , 0 , $fr_mo , $fr_dy + $x , $fr_yr ););
ENDFOR;
print "<pre>" ;
print_r ( $arrDATES );
print "</pre>" ;
?>
Thanks man, this works! I really appreciate it
I wrote this last night but never got around to posting it. It's a slightly different take on it. It takes the start and end dates and I did it like this because I thought your source dates were in this format, YYYY-mm-dd .
PHP Code:
<?php
function GetDates ( $start , $end )
{
if( preg_match ( '/^(\d{4})-(\d{2})-(\d{2})$/' , $start , $match1 )
and ( $time1 = mktime ( 0 , 0 , 0 , $match1 [ 2 ], $match1 [ 3 ], $match1 [ 1 ]))
and preg_match ( '/^(\d{4})-(\d{2})-(\d{2})$/' , $end , $match2 )
and ( $time2 = mktime ( 0 , 0 , 0 , $match2 [ 2 ], $match2 [ 3 ], $match2 [ 1 ])))
{
while( $time2 >= $time1 )
{
$rtn [] = date ( 'Y-m-d' , $time1 );
$time1 += 86400 ;
}
return $rtn ;
}
return false ;
}
print_r ( GetDates ( '2005-06-02' , '2006-06-02' ));
?>
Originally Posted by
bokeh
I wrote this last night but never got around to posting it. It's a slightly different take on it. It takes the start and end dates and I did it like this because I thought your source dates were in this format,
YYYY-mm-dd .
PHP Code:
<?php
function GetDates ( $start , $end )
{
if( preg_match ( '/^(\d{4})-(\d{2})-(\d{2})$/' , $start , $match1 )
and ( $time1 = mktime ( 0 , 0 , 0 , $match1 [ 2 ], $match1 [ 3 ], $match1 [ 1 ]))
and preg_match ( '/^(\d{4})-(\d{2})-(\d{2})$/' , $end , $match2 )
and ( $time2 = mktime ( 0 , 0 , 0 , $match2 [ 2 ], $match2 [ 3 ], $match2 [ 1 ])))
{
while( $time2 >= $time1 )
{
$rtn [] = date ( 'Y-m-d' , $time1 );
$time1 += 86400 ;
}
return $rtn ;
}
return false ;
}
print_r ( GetDates ( '2005-06-02' , '2006-06-02' ));
?>
Or this:
PHP Code:
function GetDates ( $start , $end )
{
$rtn = false ;
if( ( $time1 = strtotime ( $start )) AND ( $time2 = strtotime ( $end ) );
{
$rtn = array();
while( $time2 >= $time1 )
{
$rtn [] = date ( 'Y-m-d' , $time1 );
$time1 += 86400 ;
}
}
return $rtn ;
}
Yeah, you could do that, (I was considering that to start with) but I did it this way so I could check the input was formated correctly.
I think that strtotime() does that. It returns false if it can't sort things out or a timestamp. That would give your function more "flexibility" - although that could be a two-edged sword...
Also, the function now has a single entry and exit point.
¿two-edged or double-edged?
Voy a acabar mal...
Double-edged sword: A benefit that is also a liability, or that carries some significant but non-obvious cost or risk.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks