Link to home
Start Free TrialLog in
Avatar of t3chguy
t3chguyFlag for United States of America

asked on

Fiscal Year date range

My company's fiscal year starts on May 1 and ends on the following year's April 30.

I am trying to come up with a function to not only return each of the month/years, but also figure out a way to put this in an array to get data out based on each month during the fiscal year.

I wanted to be able to determine which fiscal year we are in based on today's date.

I also want to be able to get the date ranges for last fiscal year and next fiscal year.

Finally, if this helps, we are currently in fiscal year 2014.

Not sure where to start.

Keep in mind, this can't use date classes as I am still using PHP 5.2
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Avatar of t3chguy

ASKER

Kind of makes sense, but not sure how to apply it to my situation.
Let's try to deconstruct the problem a little.  "I also want to be able to get the date ranges for last fiscal year and next fiscal year."  Won't the date ranges always be the same: May thru April?
Avatar of t3chguy

ASKER

Yes, they will.  But I guess the part I don't know about is getting that in a function
It makes more sense to me if you put it in a class.  Then you can use the class properties to extract the information.

ERRONEOUS CODE DELETED

Open in new window

Avatar of t3chguy

ASKER

I really don't understand object oriented PHP.  Trying to understand it but it's above me.

How do I determine which fiscal year this is?  How can I determine which fiscal year last year was as well as next years fiscal year?

Ultimately what I'm trying to do is develop a report that will print the current fiscal year months and years and run through data to get certain values from a database for each month of the fiscal year with a link that will allow a user to view last fiscal year's data as well.
Uhh, check that.  It's wrong.  I'll get it fixed and get you some examples.
This looks better.
http://www.laprbass.com/RAY_temp_t3chguy.php

Basically, an object is sort of like an associative array.  You use "property names" like they were array keys, but the notation is slightly different.

if $x is an array, you would refer to the value at the key "abc" like this: $x["abc"]
if $x is an object, you would refer to the value at the property "abc" like this: $x->abc

There is an example at the end of using the properties of the object.  The advantage of using an object instead of a function is that you have three important "fiscal" data elements: year, quarter and month.  By creating the object, you get access to all three.  Object creation is accomplished with the keyword "new."  This is also called "instantiation" since the object we are creating is an "instance" of the class.

Also, upgrade your PHP ASAP.  5.2 is not supported any more, not even for security issues!

<?php // RAY_temp_t3chguy.php 2013-10-31
error_reporting(E_ALL);
echo '<pre>';

// FISCAL YEAR INFORMATION
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28282291.html

Class Fiscal
{
    public function __Construct($date='Today')
    {
        $this->input = $date;

        // REJECT BOGUS DATE STRINGS
        $ts = strtotime($date);
        if (!$ts) return FALSE;

        // CALENDAR DATE PROPERTY
        $this->date = date('Y-m-d', $ts);

        // CALENDAR MONTH KEY TO FISCAL MONTH
        $fiscal = array
        (  1 =>  9
        ,  2 => 10
        ,  3 => 11
        ,  4 => 12
        ,  5 =>  1
        ,  6 =>  2
        ,  7 =>  3
        ,  8 =>  4
        ,  9 =>  5
        , 10 =>  6
        , 11 =>  7
        , 12 =>  8
        )
        ;
        // CALENDAR MONTH KEY TO FISCAL QUARTER
        $quarts = array
        (  1 =>  3
        ,  2 =>  4
        ,  3 =>  4
        ,  4 =>  4
        ,  5 =>  1
        ,  6 =>  1
        ,  7 =>  1
        ,  8 =>  2
        ,  9 =>  2
        , 10 =>  2
        , 11 =>  3
        , 12 =>  3
        )
        ;

        // DETERMINE THE CALENDAR MONTH
        $mo = date('n', $ts);

        // DETERMINE FISCAL YEAR, MONTH AND QUARTER
        $this->fy = date('Y', $ts);
        if ($mo > 4) $this->fy++;

        $this->fm = $fiscal[$mo];
        $this->fq = $quarts[$mo];
    }
}

// SOME TESTS
$tests = array
( 'April 29, 2013'
, 'April 30, 2013'
, 'April 31, 2013'
, 'May 1, 2013'
, 'Yesterday'
, 'Today'
, 'Tomorrow'
, 'Garbage'
)
;
foreach ($tests as $td)
{
    // SHOW WHAT WE ARE TESTING
    echo PHP_EOL . $td;

    // MAKE THE OBJECT
    $dat = new Fiscal($td);

    // USE THE PROPERTIES OF THE OBJECT
    echo " IS FISCAL MONTH $dat->fm IN FISCAL YEAR $dat->fy";
}

Open in new window

Avatar of t3chguy

ASKER

Notice:  Undefined property:  Fiscal::$fm in /san-dev/htdocs/reports/newVendors.php on line 118

Notice:  Undefined property:  Fiscal::$fy in /san-dev/htdocs/reports/newVendors.php on line 118
Yep, that's from the input that says "garbage."  You can test the return value from the instantiation of the object, where it will return FALSE if the input is not a usable date.  I suppose we could also add NULL default values to the class.
I like this a little better...

<?php // RAY_temp_t3chguy.php 2013-10-31
error_reporting(E_ALL);
echo '<pre>';

// FISCAL YEAR INFORMATION
// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28282291.html

Class Fiscal
{
    public function __Construct($input='Today')
    {
        // SAVE THE INPUT VALUE
        $this->input = $input;

        // NULL PROPERTIES BEFORE DATE CONVERSION
        $this->fy = $this->fm = $this->fq = NULL;

        // REJECT BOGUS DATE STRINGS
        $this->date = 'bogus';
        $ts = strtotime($input);
        if (!$ts) return FALSE;

        // CALENDAR DATE PROPERTY
        $this->date = date('Y-m-d', $ts);

        // CALENDAR MONTH KEY TO FISCAL MONTH
        static $fiscal = array
        (  1 =>  9
        ,  2 => 10
        ,  3 => 11
        ,  4 => 12
        ,  5 =>  1
        ,  6 =>  2
        ,  7 =>  3
        ,  8 =>  4
        ,  9 =>  5
        , 10 =>  6
        , 11 =>  7
        , 12 =>  8
        )
        ;
        // CALENDAR MONTH KEY TO FISCAL QUARTER
        static $quarts = array
        (  1 =>  3
        ,  2 =>  4
        ,  3 =>  4
        ,  4 =>  4
        ,  5 =>  1
        ,  6 =>  1
        ,  7 =>  1
        ,  8 =>  2
        ,  9 =>  2
        , 10 =>  2
        , 11 =>  3
        , 12 =>  3
        )
        ;

        // DETERMINE THE CALENDAR MONTH
        $mo = date('n', $ts);

        // DETERMINE FISCAL YEAR
        $this->fy = date('Y', $ts);
        if ($mo > 4) $this->fy++;

        // DETERMINE FISCAL MONTH AND QUARTER
        $this->fm = $fiscal[$mo];
        $this->fq = $quarts[$mo];
    }
}

// SOME TESTS
$tests = array
( 'April 29, 2013'
, 'April 30, 2013'
, 'April 31, 2013'
, 'May 1, 2013'
, 'Yesterday'
, 'Today'
, 'Tomorrow'
, 'Garbage'
)
;
foreach ($tests as $td)
{
    // SHOW WHAT WE ARE TESTING
    echo PHP_EOL . $td;

    // MAKE THE OBJECT
    $dat = new Fiscal($td);

    // USE THE PROPERTIES OF THE OBJECT
    echo " IS FISCAL MONTH $dat->fm IN FISCAL YEAR $dat->fy";
}

// SHOW THE OBJECT AFTER THE GARBAGE INPUT
var_dump($dat);

Open in new window

Avatar of t3chguy

ASKER

Okay, that makes sense.

But how can I use that class thing to create a report that looks like this?  

Links to previous fiscal years.
Column headers are each month of the current fiscal year
Somewhere in the loop write a query to get the data for each month in the requested fiscal year?
The image does not seem to show any fiscal year information -- it all looks like calendar years and months to me.
Avatar of t3chguy

ASKER

That is correct, but they requested it be in fiscal year format instead.
So you basically need May-through-April (of next year) in the twelve columns?
Avatar of t3chguy

ASKER

Correct.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the points and thanks for using EE, ~Ray