如何使用奏鸣曲碳库中的当年
How to use is current year from carbon library on sonata
我是奏鸣曲的新手,我正在尝试使用 carbon 库生成今年所有记录的输出。
本文来自碳库
Carbon::isCurrentYear
没有争论
returns布尔
检查实例是否与当前时刻在同一年。
方法添加 1.22.0 无参数
以下是我尝试应用的代码
public function getIsActiveThisYear(): bool
{
$now = Carbon::isCurrentYear();
$endofyear = $endDate->year;
$startofyear = $startDate->year;
return $this->$endofyear == $now || $this->$startofyear == $now;
}
此代码导致的错误是:
isCurrentYear does not exist
只需这样做:
$date = new DateTime(); // Carbon extends the PHP DateTime class so it's the same.
$thisYear = $date->format('Y');
查看文档! :-)
https://www.php.net/manual/en/class.datetime.php
您还需要传递开始日期和结束日期
public function getIsActiveThisYear(DateTime $startDate, DateTime $endDate): bool
我是奏鸣曲的新手,我正在尝试使用 carbon 库生成今年所有记录的输出。
本文来自碳库 Carbon::isCurrentYear 没有争论 returns布尔 检查实例是否与当前时刻在同一年。 方法添加 1.22.0 无参数
以下是我尝试应用的代码
public function getIsActiveThisYear(): bool
{
$now = Carbon::isCurrentYear();
$endofyear = $endDate->year;
$startofyear = $startDate->year;
return $this->$endofyear == $now || $this->$startofyear == $now;
}
此代码导致的错误是:
isCurrentYear does not exist
只需这样做:
$date = new DateTime(); // Carbon extends the PHP DateTime class so it's the same.
$thisYear = $date->format('Y');
查看文档! :-)
https://www.php.net/manual/en/class.datetime.php
您还需要传递开始日期和结束日期
public function getIsActiveThisYear(DateTime $startDate, DateTime $endDate): bool