BoarderLine Posted September 18, 2010 Share Posted September 18, 2010 Hi, I am using this function to output a list of dates for every friday over the next 12months. This is working fine. [code=php:0] //function function nextWeeksDay($date_begin,$nbrweek) { $nextweek=array(); for($i = 1; $i <= $nbrweek; $i++) { // 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); } return $nextweek; } /// end function /// example of a select date // var $date_begin=strtotime('this Friday'); $nbrweek=52; // call function $result=nextWeeksDay($date_begin,$nbrweek); // Insert //$date1=date('Y-m-d', $date_begin); for($i = 1; $i <= $nbrweek; $i++) { //$date=$result[$i]; echo $result[$i]."<br/>"; } [/code] My problem is that when I alter the code to try and get the date for every second Friday for the next year the script freezes. [code=php:0] //function function nextWeeksDay($date_begin,$nbrweek) { $nextweek=array(); for($i = 2; $i <= $nbrweek; $i+2) { // 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); } return $nextweek; } /// end function /// example of a select date // var $date_begin=strtotime('this Friday'); $nbrweek=52; // call function $result=nextWeeksDay($date_begin,$nbrweek); // Insert //$date1=date('Y-m-d', $date_begin); for($i = 2; $i <= $nbrweek; $i+2) { //$date=$result[$i]; echo $result[$i]."<br/>"; } [/code] I really don't understand why?? Does anyone know why? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/ Share on other sites More sharing options...
BoarderLine Posted September 18, 2010 Author Share Posted September 18, 2010 Sorry forgot to add im using PHP version 5.3.3 Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1112618 Share on other sites More sharing options...
PaulRyan Posted September 19, 2010 Share Posted September 19, 2010 I believe you are looking for something like this function... <?php function get_date($month, $year, $week, $day) { # $month, $year: current month to search in # $week: 0=1st, 1=2nd, 2=3rd, 3=4th, -1=last # $day: 0=mon, 1=tue, 2=wed, 3=thu, 4=fri, 5=sat, 6=sun $startday = '1'; $delta = '0'; if($week < '0'){ $startday = date('t', mktime(0, 0, 0, $month, 1, $year)); # 28..31 $delta = '1'; } $start = mktime(0, 0, 0, $month, $startday, $year); $dstart = date('w', $start)-1; # last of the month falls on 0=mon,6=sun $offset = $day-$dstart; if ($offset<$delta){$offset+=7;} $newday = $startday+$offset+($week*7); return $month.'-'.$newday.'-'.$year; }echo get_date('10', '2010', '1', '4'); ?> That should get you heading the in right direction. You will now need to make a while loop and then use the echo part in your loop and change the month and year accordingly. Hope this helps. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1112630 Share on other sites More sharing options...
BoarderLine Posted September 19, 2010 Author Share Posted September 19, 2010 Thanks Paul, I will look into this as an alternate. However I would love to know why the function is not working as surely all logic suggests that if the the function is working correctly incrementing by +1 weeks it should also work incrementing by +2 weeks should it not? Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1112641 Share on other sites More sharing options...
PaulRyan Posted September 19, 2010 Share Posted September 19, 2010 In theory it should work, but in practice it doesn't. Your first function is using every week for 52 weeks. Your second function is using every 2 weeks for 52 weeks, so you aren't getting the desired result. I will take a proper look at your code and try to figure somthing out. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1112642 Share on other sites More sharing options...
BoarderLine Posted September 19, 2010 Author Share Posted September 19, 2010 Still can't find a solution here. I have tried changing number of weeks, incrementing by anything other than +1 but for some reason the script is freezing like it is causing a continuous loop. I have tidied up the trouble code below as the original above was a bit messy. Any assistance on what may be causing this issue or a way to debug this script is appreciated as this is causing me serious pain. [code=php:0] # function function nextWeeksDay($date_begin,$nbrweek) { $nextweek=array(); for($i = 2; $i <= $nbrweek; $i+2) { # 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); } return $nextweek; } # end function # example of a select date $date_begin=strtotime('this Friday'); $nbrweek=52; # call function $result=nextWeeksDay($date_begin,$nbrweek); for($i = 2; $i <= $nbrweek; $i+2) { echo $result[$i]."<br/>"; } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1112931 Share on other sites More sharing options...
BoarderLine Posted September 20, 2010 Author Share Posted September 20, 2010 So it seems trying to increment by +2 was just causing $date_begin to repeat in a constant loop (obviously as it was never reaching <= $nbrweek. Ugly I know however the only way I could get it to work was by doing this: [code=php:0] $date_begin=strtotime('this Friday'); $nbrweek=52; $i = 0; do { # 52 week in one year of course $nextweek[$i]=date('Y-m-d', strtotime('+'.$i.' week',$date_begin)); echo $nextweek[$i].'<br/>'; $i++; $i++; } while ($i <= $nbrweek); exit; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1113039 Share on other sites More sharing options...
DavidAM Posted September 20, 2010 Share Posted September 20, 2010 You're not actually incrementing $i, so the loop will never end // THIS for($i = 2; $i <= $nbrweek; $i+2) { # 52 week in one year of course // SHOULD BE THIS for($i = 2; $i <= $nbrweek; $i+=2) { # 52 week in one year of course Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1113075 Share on other sites More sharing options...
BoarderLine Posted September 20, 2010 Author Share Posted September 20, 2010 Legend David thanks so much. Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1113083 Share on other sites More sharing options...
pkphp Posted September 20, 2010 Share Posted September 20, 2010 elegant scripts. do you mind i take to my projects ? lol Quote Link to comment https://forums.phpfreaks.com/topic/213766-future-date-function/#findComment-1113133 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.