Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Functions

Options
  • 22-08-2007 12:04am
    #1
    Registered Users Posts: 7,041 ✭✭✭


    I having a problem with a PHP function, its not an error per se but definetly a problem. Basically the page with the function takes AGES (2min) to load and then the function doesn't run through, in fact nothing runs. The background colour changes but nothing appears on screen.

    I know its not the code within the function as if I execute it without the function it runs pefectly. I'm thinking maybe my function isn't typed right although I've checked it off numerous examples and it looks fine. Here it is anyway:

    [PHP]function disday($count,$C0,$C3,$C4,$C5,$C6){
    for($i=1;$i<365;$count){
    switch ($WD){
    case 0:
    $i=$i+$C0;
    break;
    case 3:
    $i=$i+$C3;
    break;
    case 4:
    $i=$i+$C4;
    break;
    case 5:
    $i=$i+$C5;
    break;
    case 6:
    $i=$i+$C6;
    break;
    default:
    $i=$i+0;
    }
    }[/PHP]
    [PHP]disday('$i+=3','2','2','2','2','2');[/PHP]
    I left out the parts that don't change (remember everything inside the function works).

    Is my function the problem or can it be something else?
    Thanks,
    S.


Comments

  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    The only thing I notice is that you're not incrementing $i in the third clause of your for loop.

    Perhaps you meant:

    [PHP]for($i = 1; $i < 365; $i += $count){[/PHP]

    ?

    (No idea if you did as I don't know what you're trying to accomplish.)


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    IRLConor wrote:
    The only thing I notice is that you're not incrementing $i in the third clause of your for loop.

    Perhaps you meant:

    [PHP]for($i = 1; $i < 365; $i += $count){[/PHP]

    ?

    (No idea if you did as I don't know what you're trying to accomplish.)

    I tell it to increment by three when I'm calling the function. Or am I... I'll test it out.

    Thanks.

    EDIT-
    I did as you recommended and that definetly sped it up and though stuff outside of the function is executing (I didn't before) however the stuff within it isn't. Its definitely progress, I'll mess around with it more.

    Thanks.


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Actually, I wasn't paying attention and didn't look at where you were incrementing $i in the body of the for loop.

    What are you trying to do?


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    I found the problem but don't know why its happening. To explain what I'm doing is extremly difficult so I'll just explain the bits I can and that are relevent.

    I'm counting the julian days from the start of the year.
    I convert them to date format (mm/dd/yyyy).
    Now, this is where things go funky. Because it happening inside of a function yyyy is not 2007 instead its -4713.

    It HAS to be because its inside of a function. I removed everything within the parenthesis of the function and the code within the function is the same one that works outside of the function, letter for letter, space for space its the same.

    The only difference between the working one and the other is that the non-working one is inside a function. I can't understand it.
    I've looked it up but can't find anything.

    I've been working on this program for the past month and this is the last bit. I'm hoping to finish tomorrow (today, midnight was an hour ago) and I'll be damned if this thing is going to hold me back!

    Is this a PHP glitch? Any info would be greatly appreciated.


  • Subscribers Posts: 4,076 ✭✭✭IRLConor


    Is this what you're trying to do?

    [PHP]
    // Return the number of days from the start of the Julian year
    // to the provided date
    function num_days_since_start_of_julian_year($gregorian_date) {
    $date_as_epoch = strtotime($gregorian_date);

    $day_count_date = unixtojd($date_as_epoch);
    $day_count_start_of_year = unixtojd(
    strtotime(date("01/01/Y", $date_as_epoch))
    );

    return $day_count_date - $day_count_start_of_year;
    }
    [/PHP]


  • Advertisement
  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Not really, my code is very complicated. These are the bits that are causing trouble:
    [php]$year = date("Y");
    $JD = cal_to_jd(CAL_JULIAN,01,01,$year); //Creates a julian day count from the 01/01 of the current year

    function disday(){
    for($i=1;$i<20;$i++){
    $Cday = $i+$JD; // Ads the current count (i) to the julian day to make the Julian Count go through every day
    $JDDate = jdtojulian($Cday-1); //Creates accurate date from julian day count.

    echo '<br />JDDate: '.$JDDate;
    }
    }[/php]
    Take it in and out of the function and look at the year.
    Any insight?

    EDIT-- I never globalised my necessary variables that were created outside of the function.
    Sorry.


Advertisement