sahel Posted December 4, 2008 Share Posted December 4, 2008 Hi, If someone could plz code this small php code for me. I have 4 variables to represent a startdate and enddate based on month and year. php Code: $startmonth = 3; $startyear = 2007; $endmonth = 5; $endyear = 2008; I want to loop through all months from 3-2007 to 5-2008 and do some processing for each month. Can anyone help me? Just make me the loop and ill code the process inside the loop myself. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/135472-small-loop-dont-have-time-to-code-it/ Share on other sites More sharing options...
Mark Baker Posted December 4, 2008 Share Posted December 4, 2008 So you don't have time to write these five lines: $startDate = mktime(0,0,0,$startmonth,1,$startyear); $endDate = mktime(0,0,0,$endmonth+1,1,$endyear); for($date = $startDate; $date < $endDate; $date = strtotime('+1 month',$date)) { echo 'Date is '.date('M-Y',$date).'<br />'; } I don't know how you're going to find time to write the actual processing for each month Quote Link to comment https://forums.phpfreaks.com/topic/135472-small-loop-dont-have-time-to-code-it/#findComment-705770 Share on other sites More sharing options...
sahel Posted December 4, 2008 Author Share Posted December 4, 2008 Well, i did actually get time to write it.. but it was way more that what you sent.. $startmonth = 12; $startyear = 2007; $endmonth = 11; $endyear = 2010; $completedyear = 0; for ($i = $startyear ; $i <= $endyear; $i++){ if($i < $endyear){$tendmonth=12;} else {$tendmonth=$endmonth;} if($completedyear == 1) {$tstartmonth=1;} else {$tstartmonth=$startmonth;} for ($x = $tstartmonth; $x <= $tendmonth; $x++){ echo $i."-".$x."<br/>"; $completedyear = 1; } } I guess you have much better php skills than i have Quote Link to comment https://forums.phpfreaks.com/topic/135472-small-loop-dont-have-time-to-code-it/#findComment-705781 Share on other sites More sharing options...
sasa Posted December 4, 2008 Share Posted December 4, 2008 try $startmonth = 12; $startyear = 2007; $endmonth = 11; $endyear = 2010; $completedyear = 0; for ($i = $startyear *12 + startmont; $i <= $endyear *12 + $endmonth; $i++){ echo $i % 12, '-', (int) ($i / 12); } Quote Link to comment https://forums.phpfreaks.com/topic/135472-small-loop-dont-have-time-to-code-it/#findComment-705819 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.