# Fiscal Calculator

By Eron
on January 15, 2003

Version: 0.1

Type: Function

Category: Calendars/Dates

Description: A function to generate the first and last date of a fiscal month given a month (1-12) and year.

```<?php

/*

Author:	Eron Howard, Novacoast Inc.
Date: 1/14/2003

Fiscal calculator function. Input month (1-12) and year (4 digit), returns start and end date of fiscal month.
This logic has been tested by Novacoast, however, we encourage you to test the logic yourself and take no
responsibility for results.

This is open source code. I hope it saves you time. Give us feedback at www.novacoast.com
*/

function fiscals( \$month, \$year )
{

\$month = \$month - 0;

\$months[1] = 31;
\$months[2] = 28;
\$months[3] = 31;
\$months[4] = 30;
\$months[5] = 31;
\$months[6] = 30;
\$months[7] = 31;
\$months[8] = 31;
\$months[9] = 30;
\$months[10] = 31;
\$months[11] = 30;
\$months[12] = 31;

\$startyear = \$year;
if( \$month == 1 || \$month == 2 )
{
\$startyear =\$year-1;

}

\$unixYearStart = mktime (5, 0, 0, 3, 1, \$startyear );

\$monthstart[3] = getDate( \$unixYearStart );

if( \$monthstart[3]["weekday"] == "Saturday" ) \$addDays = 7;
if( \$monthstart[3]["weekday"] == "Sunday" ) \$addDays = 6;
if( \$monthstart[3]["weekday"] == "Monday" ) \$addDays = 5;
if( \$monthstart[3]["weekday"] == "Tuesday" ) \$addDays = 4;
if( \$monthstart[3]["weekday"] == "Wednsday" ) \$addDays = 3;
if( \$monthstart[3]["weekday"] == "Thursday" ) \$addDays = 2;
if( \$monthstart[3]["weekday"] == "Friday" ) \$addDays = 1;

\$currentday = 1+ \$addDays; //1st week
\$currentmonth = 3;
for( \$i=0; \$i<20; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[3] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;
//echo "HERE: ".\$currentmonth." ".\$currentday."<BR>";
\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[4] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[4] = getDate( \$unixNextWeek );
//echo "HERE: ".\$currentmonth." ".\$currentday."<BR>";
if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

//	echo "HERE: ".\$currentmonth." ".\$currentday."<BR>";
\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[5] = getDate( \$unixNextWeek );

for( \$i=0; \$i<35; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[5] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[6] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[6] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[7] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[7] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[8] = getDate( \$unixNextWeek );

for( \$i=0; \$i<35; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[8] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[9] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[9] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[10] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[10] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[11] = getDate( \$unixNextWeek );

for( \$i=0; \$i<35; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[11] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[12] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[12] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[1] = getDate( \$unixNextWeek );

for( \$i=0; \$i<28; \$i++ )
{
if( \$currentday > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

}

if( \$month == 1 || \$month == 2 )
\$startyear = \$year;
else
\$startyear = \$year+1;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );

\$monthend[1] = getDate( \$unixNextWeek );

if( \$currentday+1 > \$months[\$currentmonth] )
{
\$currentday = 1;
if( \$currentmonth == 12 ) \$currentmonth =1;
else \$currentmonth++;
}
else \$currentday++;

\$unixNextWeek = mktime( 5, 0, 0, \$currentmonth, \$currentday, \$startyear );
\$monthstart[2] = getDate( \$unixNextWeek );

\$day = 28;
if( \$startyear % 4 == 0 ) \$day = 29;

\$unixNextWeek = mktime( 5, 0, 0, 2, \$day, \$startyear );

\$monthend[2] = getDate( \$unixNextWeek );

\$fiscal[0] = \$monthstart[\$month];
\$fiscal[1] = \$monthend[\$month];

return \$fiscal;
//	echo \$monthstart[\$month]["month"]." ".\$monthstart[\$month]["mday"]." ".\$monthstart[\$month]["weekday"]." ".\$monthstart[\$month]["year"];
//	echo "<BR>";
//	echo \$monthend[\$month]["month"]." ".\$monthend[\$month]["mday"]." ".\$monthend[\$month]["weekday"]." ".\$monthend[\$month]["year"];
//	echo "<BR><BR>";
}

?>```
﻿