• PHP Help PHP Coding
  • [RESOLVED] Logic Overflow error (in my head) on setting up a fiscal year test loop

Hi All,

OK, I need a terrabyte of ram installed in my head to figure this one out completely.

We have teachers that sign up for free videos and I need to verify their signup dates and feedback dates based on a Fiscal calendar not a year calendar. (IE their fiscal is July 1 - June 30).

So far I tested if now is after or before July 1, (current year) but my logic fails on the test if its the next year but before the fiscal ends.

IE We are in our new current fiscal (July 1, 2007 - June 30, 2008).

Here is some tests I tried so you can see what I have done so far.

$year = date("Y");
//$NowUnix  = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
//echo '$NowUnix = '.$NowUnix.'<br />';
$now = date("m,d Y");
//echo 'Now is '.$now.'<br />';
$UnixNow = mktime(0,0,0,$now);
echo '$UnixNow = '.$UnixNow.'<br />';
$StartFiscal = mktime(0,0,0,7,1,$year);

if($UnixNow>=$StartFiscal) {
echo 'It is a new fiscal<br />';
$FiscalStart = mktime(0,0,0,7,1,$year);
} else {
echo 'The fiscal has not ended yet<br />';
}

I need to dynamically create a date range that runs from July1, to June 30 and resets each year. Then I can test their date stamps from the database and send them to the appropriate places.

Does that make sense?

Thanks in advance,

Don

    Hi,

    You are in the current FISCAL year if ...

    either: Month >= July AND Year == CURRENT_YEAR
    or: Month < July AND Year == CURRENT_YEAR + 1

    You've got to test both conditions.

    P.

      Hi Paul,

      If I follow you, I need to set CURRENT_YEAR at the start of each calendar year in my code to get this to work. (IE I set CURRENT_YEAR=$year after 6/30/2007)

      either: Month >= July AND Year == CURRENT_YEAR
      or: Month < July AND Year == CURRENT_YEAR + 1

      Because in January, my $year=2008

      $year = date("Y"); //this would need to be set once per fiscal year, correct?
      

      and this would fail.

      Or am I complicating your idea?

      Thanks,
      Don

        Hi there,

        Not 100% sure of exactly what you need to achieve, but I thought it involved testing to whether a particular date (timestamp, actually) was in a particular (current) fiscal year.

        Here's what I said again, but in a bit more detail. Still might not be exactly what you're after but at least it clears up how to test a given date.

        Look at the function first ... the rest is just testing some sample dates ...

        <?php
        // The year we are currently in
        $CURRENT_YEAR = date("Y");
        
        // A set of test dates as timestamps
        $timestamps = array(
        	strtotime('-3 month') // 3 months ago today
        ,	strtotime('-2 month') // 2 months ago today
        ,	strtotime('-1 month') // etc.
        ,	strtotime('now')
        ,	strtotime('+1 month')
        );
        
        foreach($timestamps as $timestamp){
        	if(in_fiscal_year($CURRENT_YEAR, $timestamp)){
        		echo date('d M Y', $timestamp).' is in the current fiscal year.<br>';
        	} else {
        		echo date('d M Y', $timestamp).' is NOT in the current fiscal year.<br>';
        	}
        }
        
        // Function args
        // * $fiscal_year ... here 1999 means 1 Jul 1999 - 30 Jun 2000
        //   We've used the current year (2007) above
        // * $timestamp ... the timestamp of the date you want to test
        function in_fiscal_year($fiscal_year, $timestamp){
        
        // This is the first month in your fiscal year
        // (you could make this a parameter too, if necessary)
        $FIRST_MONTH = 7; // July
        
        $month = (int) date("n", $timestamp);
        $year = (int) date("Y", $timestamp);
        
        return (
        	($month >= $FIRST_MONTH && $year == $fiscal_year)
        ||	($month < $FIRST_MONTH && $year == $fiscal_year + 1)
        );
        }
        ?>

        P.

          Hi Paul,

          Sweet function! This is helping me understand a lot better.

          One last question. CURRENT_YEAR is now 2007 and FIRST_MONTH is 7, but when January rolls around, CURRENT_YEAR will be 2008 and I think, from my tests on the function, it will say that Jan-Jun is not in the current fiscal and it would be.

          Does that make sense?

          In your code I changed the FIRST_MONTH to 9

          $FIRST_MONTH = 9;
          

          to see if it would show may-aug in the current fiscal (which they would be if our fiscal started in September, like I changed it). But now it shows...

          02 May 2007 is NOT in the current fiscal year.
          02 Jun 2007 is NOT in the current fiscal year.
          02 Jul 2007 is NOT in the current fiscal year.
          02 Aug 2007 is NOT in the current fiscal year.
          02 Sep 2007 is in the current fiscal year.

          If I can get past this, I think you have it beat. Unless I am wrong, which I very well could be.

          Thanks for your patience,
          Don

            Hi, the list ...

            02 May 2007 is NOT in the current fiscal year.
            02 Jun 2007 is NOT in the current fiscal year.
            02 Jul 2007 is NOT in the current fiscal year.
            02 Aug 2007 is NOT in the current fiscal year.
            02 Sep 2007 is in the current fiscal year.

            ... is correct since we used 2007 as the $fiscal_year, which would run from 1 Sep 2007 - 31 Aug 2008.

            You could have a different function to determine which fiscal year you are currently in ...

            <?php
            // Function args
            // * $first_month ... 1 - Jan, 2-Feb, etc.
            function current_fiscal_year($first_month){
            
            $current_month = (int) date("n");
            $current_year = (int) date("Y");
            
            return ($current_month >= $first_month)? $current_year: $current_year - 1;
            }
            ?> 

            Haven't fully tested this one though (check out choosing Jan or Dec for start months to look for bugs) but you get the idea.

            Now you could determine which fiscal year you are currently in

            <?php
            $FIRST_FISCAL_MONTH = 9; //September
            $CURRENT_FISCAL_YEAR = current_fiscal_year($FIRST_FISCAL_MONTH); 
            ?>

            Does that clear it up?
            P.

            PS here's the function extended to include variable 'first month's ...

            <?php
            // Function args
            // * $fiscal_year ... here 1999 means 1 - $first_month - 1999 - 30 - ($first_month - 1) -  2000
            // * $first_month ... 1 - Jan, 2-Feb, etc.
            // * $timestamp ... the timestamp of the date you want to test
            function in_fiscal_year($fiscal_year, $first_month,  $timestamp){
            
            $month = (int) date("n", $timestamp);
            $year = (int) date("Y", $timestamp);
            
            return (
                ($month >= $first_month && $year == $fiscal_year)
            ||    ($month < $first_month && $year == $fiscal_year + 1)
            );
            }
            ?> 

              I'm wondering if things might be simplified by just getting the fiscal year number. Typically, if a fiscal year starts on July 1, then on 2007/07/01 the fiscal year is 2008. If this is the way you number fiscal years, then you could create a fairly simple function to return the fiscal year for any date value, then use that value for fiscal year comparisons.

              function fiscalYear($unixTime)
              {
                 return (date('n',$unixTime) > 6) ? 
                         date('Y',$unixTime) + 1 :
                         date('Y',$unixTime);
              }
              

              If, on the other hand, you consider 2007/07/01 to be the start of the 2007 fiscal year (therefore 2007/06/30 would have been the end of the 2006 fiscal year), then just change the "+ 1" to a "- 1".

                Yes, that does help a lot. I will play with this until it is totally clear.

                One quick question. I dont understand the code in this line (IE in English, what is it doing?)

                return ($current_month >= $first_month)? $current_year: $current_year - 1;
                

                I see that I have been doing timestamp comparisons the hard way. Once I have this down, I see where it will be a lot easier.

                Thanks,
                Don

                [edit]

                Thanks NogDog, I see your logic also. For my use on the web, I dont need to name the fiscal, just test the teachers timestamps against it to see if they fall within our requirements. I will have to play with both versions to see what works best, but both of you approached it WAY better then I would have.

                I LOVE this forum! Everyone here is more knowledgeable and more willing to explain and help than any other forum I visit. Period!

                Thanks,
                Don

                  The "ternary operator" ("?:") is a sort of shorthand way of doing an if/else. The basic syntax is:

                  (<expression>) ? <value_if_expression_is_true> : <value_if_expression_is_false>
                  

                  So, the following will always echo "bar":

                  echo ("foo" == "bar") ? "foo" : "bar";
                  

                  It is functionally equivalent to doing:

                  if("foo" == "bar")
                  {
                     echo "foo";
                  }
                  else
                  {
                     echo "bar";
                  }
                  

                    Thanks to both of you. I will use these methods and replace my old way of doing it.

                    Owe ya.
                    Don

                      Write a Reply...