PHP Calculate Start of Financial Year
Asked Answered
B

7

5

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.

Brentonbrentt answered 4/11, 2013 at 16:39 Comment(1)
Easiest and best solution would be chumkiu's answer bellow. See demo.Bitolj
S
6

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") );
Sices answered 4/11, 2013 at 17:4 Comment(1)
Thanks - great one line solution. I'll remember this one for future reference ;)Brentonbrentt
I
1

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

Iphagenia answered 4/11, 2013 at 16:43 Comment(5)
Yeah, I did use the disclaimer just for fun, but hopefully would stimulate a similar solution, BTW the php eval site was unknown to me thanks for thatIphagenia
It returns Thursday 10th January (2013-01-10), but that is wrong, this year the first Thursday was 2013-01-03...Brentonbrentt
@ShaunHare Adjusted: 3v4l.org/on5ls although no idea why this year returns different results when it's the same thing..Mcleroy
<?php $date = date("Y")."-01-01"; if (date("Y-m-d", strtotime("first thursday of january this year")) != $date){ $date = date("Y-m-d", strtotime("last thursday of december last year")); } print($date);Colewort
@ShaunHare: years that have Thursday on 1st, return 8th (2004, 2009, etc).Bitolj
C
1
<?php

$date = date("Y")."-01-01";
while(date("l", strtotime($date))!="Thursday")){
    $date = date("Y-m-d", strtotime("-1 day", strtotime($date)));
}
Colewort answered 4/11, 2013 at 16:48 Comment(3)
That is a more succinct version of my original function, but I feel this must be possible without the need to loop at all...Brentonbrentt
well the maximum iterations for this loop is only 6 so i don't think it is inefficient. can't think of anything else unless strtotime supports something like "closest Thursday to New Years Eve" which i doubtColewort
Well yes, you need a return or break in there but get the point lePunk was making...Brentonbrentt
N
0

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);
     }
}
Nursling answered 4/11, 2013 at 16:54 Comment(0)
C
0

If you want to use your modifier approach, try :

  $modifer = ($day_num>3) ? $day_num-4 :  $day_num+3;
Corrective answered 4/11, 2013 at 16:57 Comment(0)
S
0

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();
Schifra answered 4/11, 2013 at 17:25 Comment(0)
T
0
<?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";

?>
Tovatovar answered 23/5, 2014 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.