Question

The company I'm working for has a financial year that starts on 1st January if that is a Thursday, otherwise starts on the last Thursday of the previous year.

I've written a function that does this but it seem inefficient needing to loop:

function get_start_of_financial_year() {
    $date = date('Y').'-01-01';
    $correct_day = false;
    while(!$correct_day) {
        $day_num = date('N', strtotime($date));
        if($day_num==4) return $date;
        $date = date('Y-m-d', strtotime($date.' -1 day'));
    }
}

I've been trying something like this:

function get_start_of_financial_year() {
    $date = date('Y').'-01-01';
    $day_num = date('N', strtotime($date));
    $modifer = 4 - $day_num;
    return date('Y-m-d', strtotime($date.' -'.$modifer.' days'));
}

However this doesn't work. I know I'm doing something wrong when calculating my modifier, but what?

I've had a look at other similar questions / answers on here and are all slightly different so I think this is a genuine new question.

Was it helpful?

Solution

According to the doc

"'last' dayname" takes the last dayname from the current day. (Example: "last wed july 2008" means "2008-06-25"; "july 2008" first sets the current date to "2008-07-01" and then "last wed" moves to the previous Wednesday which is "2008-06-25").

So your case is

function get_start_of_financial_year($year) {
    // get the first Thursday before 2 Jan of $year
    return date("Y-m-d", strtotime("last Thursday $year-01-02"));
}

echo get_start_of_financial_year( date("Y") );

OTHER TIPS

Just as fun I throw this into the mix for you

echo date('l jS F (Y-m-d)', strtotime('first thursday january this year'));

Try it You can then check if it is 1st ?

obviously you will need the format correct for checking etc

I love these PHP quirks

<?php

$date = date("Y")."-01-01";
while(date("l", strtotime($date))!="Thursday")){
    $date = date("Y-m-d", strtotime("-1 day", strtotime($date)));
}

The php date function actually provides a lot of easy tools to do this! I would recommend tackling your problem using the following code:

/** 
* Calculate the start of the financial year given a certain year 
* @param year the current year
* @returns the date the current financial year starts
*/
function getStartOffFinancialYear(String year) {
     // Get the timestamp of this years Jan 1st
     $timestamp = strtotime(year+'-01-01');

     // Check what day it is (you will get an integer)
     $day = date('w', $timestamp)

     if($day == 4) {         
         // If it's a thursday, we are done!
         return $timestamp;
     } else {
         // Else we take the previous thursday :)
         return strtotime("last thursday",$timestamp);
     }
}

there is the answer that work for me, I found it here: get the last thursday before a date in PHP

function get_start_of_financial_year() {
$date = strtotime('2014-01-01');
if(date('N',$date) != 4)
    return date('Y-m-d', strtotime("last thursday",$date));
else
    return $date;
}

echo get_start_of_financial_year();

If you want to use your modifier approach, try :

  $modifer = ($day_num>3) ? $day_num-4 :  $day_num+3;
<?php
$cur_dt = date('Ymd', mktime(0, 0, 0, date("m")  , date("d"), date("Y")));
//$cur_dt = date('Ymd', mktime(0, 0, 0, 5  , 10, 2013));
//$cur_dt = date('Ymd', mktime(0, 0, 0, 2  , 10, 2013));
$cur_mm="";
$cur_yy="";
$fin_st_dd="";
$fin_st_mm="";
$fin_st_yy="";
$fin_nd_dd="";
$fin_nd_mm="";
$fin_nd_yy="";
$cur_mm= substr(trim($cur_dt),4,2);
$cur_yy= substr(trim($cur_dt),0,4);


if ( $cur_mm >= "04" && $cur_mm <= "12" )
{
    $fin_st_dd="01";
    $fin_st_mm="04";
    $fin_st_yy=$cur_yy;
    $fin_nd_dd="31";
    $fin_nd_mm="03";
    $fin_nd_yy=$cur_yy+1;
}

if ($cur_mm >= "01" && $cur_mm <= "03")
{
    $fin_st_dd="01";
    $fin_st_mm="04";
    $fin_st_yy=$cur_yy-1;
    $fin_nd_dd="31";
    $fin_nd_mm="03";
    $fin_nd_yy=$cur_yy;
}

$fin_st = date('Ymd', mktime(0, 0, 0, $fin_st_mm , $fin_st_dd, $fin_st_yy));
$fin_nd = date('Ymd', mktime(0, 0, 0, $fin_nd_mm , $fin_nd_dd, $fin_nd_yy));


echo "\nCurrent Date: $cur_dt\n";
echo "\nFinancial Year From : $fin_st To : $fin_nd\n";
//echo "\n$fin_nd\n";
//echo "\n$fin_st_mm";
//echo "\n$fin_st_yy\n";
//echo "\n$fin_st_mm";
//echo "\n$fin_nd_mm";
//echo "\n$fin_nd_yy\n";

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top