Solved

Fiscal Year date range

Posted on 2013-10-31
19
711 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:t3chguy
  • 11
  • 8
19 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39614761
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39614770
Kind of makes sense, but not sure how to apply it to my situation.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39614845
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?
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39614851
Yes, they will.  But I guess the part I don't know about is getting that in a function
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39614926
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

0
 
LVL 1

Author Comment

by:t3chguy
ID: 39614950
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39614976
Uhh, check that.  It's wrong.  I'll get it fixed and get you some examples.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39615003
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

0
 
LVL 1

Author Comment

by:t3chguy
ID: 39615083
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
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39615109
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39615138
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

0
 
LVL 1

Author Comment

by:t3chguy
ID: 39615139
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?
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39615141
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39615182
The image does not seem to show any fiscal year information -- it all looks like calendar years and months to me.
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39615193
That is correct, but they requested it be in fiscal year format instead.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39615201
So you basically need May-through-April (of next year) in the twelve columns?
0
 
LVL 1

Author Comment

by:t3chguy
ID: 39615206
Correct.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39615460
See the GetMonths() method.  Give it a fiscal year and it will return an array of months and calendar years.

<?php // RAY_temp_t3chguy_2.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];
    }

    public function getMonths($year='2014')
    {
        $months = array
        (  1 => array('May', 1)
        ,  2 => array('Jun', 1)
        ,  3 => array('Jul', 1)
        ,  4 => array('Aug', 1)
        ,  5 => array('Sep', 1)
        ,  6 => array('Oct', 1)
        ,  7 => array('Nov', 1)
        ,  8 => array('Dec', 1)
        ,  9 => array('Jan', 0)
        , 10 => array('Feb', 0)
        , 11 => array('Mar', 0)
        , 12 => array('Apr', 0)
        )
        ;
        foreach ($months as $key => $sub)
        {
            $cyr = $year - $sub[1];
            $out[$key] = $sub[0] . ' ' . $cyr;
        }
        return($out);
    }
}

// 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);

// GET AN ARRAY OF MONTHS AND CALENDAR YEARS FOR A GIVEN FISCAL YEAR
$arr = $dat->getMonths('2013');
print_r($arr);

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39631437
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now