PHP计算财政年度的开始
我工作的公司的财政年度从1月1日开始,如果是星期四,则从上一年的最后一个星期四开始 我编写了一个函数来实现这一点,但由于需要循环,它似乎效率低下:PHP计算财政年度的开始,php,date,financial,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));
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'));
}
然而,这不起作用。我知道在计算修饰符时我做错了什么,但是什么呢
我已经看了这里的其他类似问题/答案,它们都略有不同,所以我认为这是一个真正的新问题。同样有趣的是,我把它加入到你的组合中
echo date('l jS F (Y-m-d)', strtotime('first thursday january this year'));
试一试,然后检查它是否是第一个
显然,您需要正确的格式进行检查等
我喜欢这些PHP怪癖
PHP日期函数实际上提供了许多简单的工具来实现这一点!
<?php
$date = date("Y")."-01-01";
while(date("l", strtotime($date))!="Thursday")){
$date = date("Y-m-d", strtotime("-1 day", strtotime($date)));
}
我建议使用以下代码解决您的问题:
/**
* 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);
}
}
如果要使用修改器方法,请尝试:
$modifer = ($day_num>3) ? $day_num-4 : $day_num+3;
根据
“'last'dayname”从当前日期获取最后一天的名称。(例如:“2008年7月最后一个星期三”表示“2008-06-25”;“2008年7月”首先将当前日期设置为“2008-07-01”,然后“最后一个星期三”移动到上一个星期三,即“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") );
我在这里找到了一个对我有用的答案:
是的,我使用免责声明只是为了好玩,但希望能刺激类似的解决方案,顺便说一句,php eval网站对我来说是未知的,感谢它于1月10日(2013-01-10)周四返回,但这是错误的,今年的第一个星期四是2013-01-03…@ShaunHare调整:虽然不知道为什么今年的在同一件事上返回不同的结果…@ShaunHare:years有星期四在1日,返回8日(2004、2009等)。这是我原来函数的更简洁版本,但是我觉得这在根本不需要循环的情况下是可能的……这个循环的最大迭代次数只有6次,所以我不认为这是低效的。除非Strotime支持类似于“离除夕最近的星期四”的东西,否则我想不出还有什么其他的东西。是的,你需要一个回归或突破,但得到了lePunk的观点…最简单和最好的解决方案是。谢谢-很好的单线解决方案。我会记住这个,以备将来参考;)
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();
<?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";
?>