PHP计算财务年度的开始

时间:2013-11-04 16:39:19

标签: php date financial

我工作的公司有一个财政年度,从1月1日开始,如果是星期四,否则从上一年的最后一个星期四开始。

我编写了一个执行此操作的函数,但需要循环似乎效率低下:

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'));
    }
}

我一直在尝试这样的事情:

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'));
}

然而,这不起作用。我知道在计算我的修饰语时我做错了什么,但是什么?

我在这里看过其他类似的问题/答案,并且都略有不同,所以我认为这是一个真正的新问题。

7 个答案:

答案 0 :(得分:6)

根据doc

  

“'last'dayname”获取当天的最后一个日期名称。 (例如:“2008年7月上旬”是指“2008-06-25”;“2008年7月”首先将当前日期设置为“2008-07-01”,然后“上次结婚”移至上一个周三,即“2008年7月” -06-25" )。

所以你的情况是

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

答案 1 :(得分:1)

同样有趣的我把它扔进了混合

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

尝试一下你可以检查它是否是第一个?

显然你需要格式正确的检查等

我喜欢这些PHP怪癖

答案 2 :(得分:1)

<?php

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

答案 3 :(得分:0)

php日期函数实际上提供了很多简单的工具来做到这一点! 我建议使用以下代码解决您的问题:

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

答案 4 :(得分:0)

如果您想使用修饰符方法,请尝试:

  $modifer = ($day_num>3) ? $day_num-4 :  $day_num+3;

答案 5 :(得分:0)

答案对我有用,我在这里找到了它: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();

答案 6 :(得分: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";

?>
相关问题