Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date for Next 30 Days Loop 1

Status
Not open for further replies.

Tama

MIS
Jun 6, 2001
121
NZ
I know this should be straight forward but coding dates always mess with my head.

All I want to do is have a loop which cycles through today and the next 30 days, generating $Day(2 digits), $Month(2 digits) and $Year(4 digits) for each cycle.

Any help would be greatly appreciated.

Cheers
Tama

I do my sums on fingers and thumbs.
 
Code:
<?php
$x = 0 ;
$today = date("m")."/".date("d")."/".date("Y") ;
for ( $year = date("Y");$year < date("Y") + 1; $year++ ) {
	for ( $month = date("m");$month < date("m") + 2;$month++ ) {
		for ( $day = date("d");$day <= date("d")+31;$day++ ) {			
					
			if ( $x == 30 ) exit() ;
			$day  = $day++  ;
			if ( $day > 31 ) {// change this condition depending on the no of days of the current month				
				$day = 1 ; 
				$month++ ;
			}
			$date = $month."/".$day."/".$year ;
			echo $date."<br>" ;
			$x++ ;
			 
		}
	}
}

?>

Note: Code not tested

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
tested, modified slightly to correct the calculated figured which because they aren't from date() are only single digit.

Code:
<?php
$x = 0 ;
//$today = date("m")."/".date("d")."/".date("Y") ;
for ( $year = date("Y");$year < date("Y") + 1; $year++ ) {
    for ( $month = date("m");$month < date("m") + 2;$month++ ) {
        for ( $day = date("d");$day <= date("d")+31;$day++ ) {            
                    
            if ( $x == 30 ) exit() ;
            $day  = $day++  ;
            if ( $day > 31 ) {// change this condition depending on the no of days of the current month                
                $day = 1 ; 
                $month++ ;
            }
            // modded in str_pad because you stop using date() and start calculating figures
            $date = str_pad($month,2,"0",STR_PAD_LEFT)."/".str_pad($day,2,"0",STR_PAD_LEFT)."/".$year ;
            echo $date."<br>" ;
            $x++ ;
             
        }
    }
}

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Code:
if ( $day > 31 ) {// change this condition depending on the no of days of the current month

or instead of having 12 different files for each month you could use the t argument of the date() function to get the number of days in that month.

----------------------------------------------------------------------------
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
 
And yes, you will alos have to take care for the december month, when after that the year is changning :)



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Much shorter, no need to think about leap years, number of days in a month:
Code:
# get current UNIX timestamp
$now = time();
echo(date('m/d/Y',$now).'<br>');
# add multiples of days 60seconds * 60minutes * 24hours * number of days
# just is trouble when the script runs over midnight
for($i=1; $i<=30; $i++) {
	echo(date('m/d/Y',$now+(60*60*24*$i)).'<br>');
}
 
Thank you everyone - my e-mail seems to have jammed for a while so I only just got the alerts to your replies.

I had started working on my own loop last night which was similar to DRJ478's solution but quite a bit uglier. I took the current time() and added 86,400 to it for every loop. It seemed to work but I'll use DRJ478's method as is has a smaller code footprint.

My original attempts started to look like far messier/ more complex versions of spookies code - I was tying myself in knots with the number of days in every month.

Thanks again - hopefully I can get the whole script fully functional tonight.

Tama

I do my sums on fingers and thumbs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top