Php 按给定月份获取财政年度

Php 按给定月份获取财政年度,php,Php,财政年度从4月1日开始,我们在1月,因此1月进入2020年,2月和3月也是如此。因此,如果我现在使用四月,它应该在2019年。目前我使用了下面的代码,但它没有按照我想要的方式工作 $month =01; if ( $month >= 4 ) { $year = date('Y'); } else { $year = date('Y')-1; } echo $year; 因此,当我将月份设置为1月份时,年份显示为2019年,但实际上应

财政年度从4月1日开始,我们在1月,因此1月进入2020年,2月和3月也是如此。因此,如果我现在使用四月,它应该在2019年。目前我使用了下面的代码,但它没有按照我想要的方式工作

    $month =01;
   if ( $month >= 4 ) {
     $year = date('Y');
   }
   else {
    $year = date('Y')-1;
   }
   echo $year;
因此,当我将月份设置为1月份时,年份显示为2019年,但实际上应该是2020年


是否可以用PHP设置年份,以便月份从4月开始到3月底结束,因此当我输入月份时,我将获得正确的年份?

我希望这将解决您的问题

$date=date_create("2020-05-13");

echo "<br> Month: ".date_format($date,"m");
if (date_format($date,"m") >= 4) {//On or After April (FY is current year - next year)
    $financial_year = (date_format($date,"Y")) . '-' . (date_format($date,"y")+1);
} else {//On or Before March (FY is previous year - current year)
    $financial_year = (date_format($date,"Y")-1) . '-' . date_format($date,"y");
}

echo "<br> FY ".$financial_year;
$date=创建日期(“2020-05-13”);
echo“
月:“.date\u格式($date,“m”); 如果(date_格式($date,“m”)>=4{//在4月或之后(FY为本年度-下一年度) $financial_year=(日期格式($date,“Y”)。-'(日期格式($date,“Y”)+1); }else{//在3月或之前(财年为上一年度-本年度) $financial_year=(日期格式($date,“Y”)-1)。'-'.date_格式($date,“Y”); } echo“
FY.”财政年度;
我想我已经解决了这个问题。欢迎提出任何意见

$month =01;
$current_month = date('m');
if ($current_month >= '01' && $current_month < '4'){

   if ($month >= '01' && $month < '04'){
      $year = date('Y');
   } 

   if ($month >= 4){
      $year = date('Y')-1;
   }
} 

if ($current_month >= 4){
   if ($month >= 4){
      $year = date('Y');
   }

   if ($month < 4){
      $year = date('Y')+1;
   }
}

echo $year;
$month=01;
$current_month=日期('m');
如果($current\u month>='01'&&$current\u month<'4'){
如果($month>='01'&&$month<'04'){
$year=日期('Y');
} 
如果($month>=4){
$year=日期('Y')-1;
}
} 
如果($当前月份>=4){
如果($month>=4){
$year=日期('Y');
}
如果($month<4){
$year=日期('Y')+1;
}
}
echo$年;

大致如下:

$now = strtotime('now');
$firstApril = strtotime('1st april');
$year = ($now < $firstApril) ? date('Y')-1 : $date('Y');
return $year;
$now=strotime('now');
$firstApril=strottime(“4月1日”);
$year=($now<$firstApril)?日期('Y')-1:$date('Y');
返回$year;

这将比较时间戳,如果时间戳小于今年的4月1日,则与前一年的时间戳进行比较。

尝试以下操作:
Carbon::now()->子月(3)->年

这样,1月、2月和3月的日期将返回2019年,4月将开始返回2020年。

您可以尝试以下方法:

 $month =01;
 if($month>=4&&$month<=12){
     $year=date('Y')-1;
 }
 if($month>=1&&$month<=3){
     $year=date('Y');
 }
   echo $year;
$month=01;

如果($month>=4&&$month=1&&$month这不是一个直接的答案,但是如果您为您的企业处理一个财政年度的多个财务/事务数据,那么您的处境可能与几年前的我相似

请随时查看此库->

对于单个检查来说,这是一种过分的做法(尽管相当轻量级),但是如果您有更多的需求,它可能会很有用

需要财政年度的开始日期和财政年度的类型(在您的情况下,它是
日历
,根据库的规范),并提供结束日期、期间范围等

返回DateTimeImmutable对象或DatePeriod,具体取决于所使用的方法(仅为
业务
类型财务年度获取日期、期间或周)

在您的情况下,用例如下

require_once __DIR__ . '/vendor/autoload.php';

// DateTimeAdapter
// If instantiating with string, it must be of ISO-8601 format 'YYYY-MM-DD'
$startDate = new \DateTime('2019-04-01');

$fy = new \RoussKS\FinancialYear\DateTimeAdapter('calendar', $startDate);

// January is the 10th period for a financial year based on 12 periods starting at 2019-04-01.
// First date of the period would give you  2020-01-01
echo $fy->getFirstDateOfPeriodById(10)->format('Y'); // 2020

请随意测试并提出建议

以下是如何获得当前财政年度的开始日期(开始日期4月1日)

公共函数getFinancialYear(){
$today=碳::today();
$month=$today->format('m');
$year=$today->format('Y');
$firstApril='四月一日'。$year;
$financialYear=新碳排放量(4月1日);
如果($month<4){
$financialYear->subYear();
}
返回$financialYear;
}

你是这么说的,如果我现在使用四月,它应该在2019年。这意味着2019年的财政年度将在4月1日结束。是吗?是的。每年的财政年度将从4月1日开始。当我将月份设置为1月1日时,你也是这么说的,年份显示为2019年,但实际上应该是2020年。怎么回事?财政年度仍将持续到2020年4月1日删除。那么为什么1月显示为2020年呢?您的代码是正确的,但您的解释与您想要实现的目标不一致。1月、2月、3月和最后4月的年输出应该是什么?因为如果我想从mysql中提取收入或支出等数据,应该是2020年。我也认为我是found回答。请检查我将在2019年5月发布的答案。请查看@vivek_23
public function getFinancialYear(){

    $today = Carbon::today();
    $month = $today->format('m');
    $year = $today->format('Y');

    $firstApril = 'first day of April '.$year;

    $financialYear = new Carbon($firstApril);

    if($month < 4){
        $financialYear->subYear();
    }

    return $financialYear;
    
}