2013-11-04 62 views
5

我正在工作的公司的財政年度從1月1日開始,如果這是一個星期四,否則從上一年的最後一個星期四開始。PHP計算財政年度開始

我寫做這個,但它似乎需要低效循環功能:

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

但是這不工作。我知道我在計算修改器時做錯了什麼,但是什麼?

我看了一下放在這裏其他類似的問題/答案,都略有不同,所以我認爲這是一個真正的新問題。

+1

最簡單,最好的辦法是[chumkiu的回答波紋管(http://stackoverflow.com/a/19773014/67332)。 ** [查看演示](https://eval.in/60243)**。 –

回答

6

按照doc

「」 last'dayname「取當天的最後一天名稱。 (例如:「2008年7月」是指「2008年6月25日」;「2008年7月」首先將當前日期設置爲「2008年7月1日」,然後「上一次婚姻」移至前一個星期三「2008年-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")); 
+3

布拉沃,這應該是被接受的答案。 –

+0

謝謝 - 偉大的一線解決方案。我會記住這個以備將來參考;) –

1

一樣的樂趣我扔到組合爲您

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

這種嘗試它,那麼你可以檢查,如果它是第一個?

顯然,你需要的格式正確檢查等等

我喜歡這些PHP怪癖

+0

http://3v4l.org/tFvVV並非所有版本由於某種原因,=) – MackieeE

+0

是啊,我也使用聲明只是爲了好玩,但希望將刺激類似的解決方案,BTW的PHP網站的eval是未知的我感謝那 –

+0

它返回週四1月10日(2013年1月10日),但這是錯誤的,今年第一個星期四是2013年1月3日... –

1
<?php 

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

這是我原來的函數的一個更簡潔的版本,但我覺得這一定是可能的,不需要循環... –

+0

以及此循環的最大迭代次數只有6次,所以我不認爲它是低效的。想不到其他任何東西,除非strtotime支持像「最接近星期四到除夕」我懷疑 – lePunk

+0

對我來說,這看起來像無盡的循環,如果今天不是星期四... –

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

如果你想用你的修改方法,嘗試:

$modifer = ($day_num>3) ? $day_num-4 : $day_num+3; 
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(); 
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"; 

?> 
相關問題