Jump to content

future date function


BoarderLine

Recommended Posts

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.