我有mySQL数据库表字段:
description
unit
start_date
end_date
例如:
开始日期:2019/05/07
结束日期:10-08-2021
我如何计算年数?
答案 0 :(得分:2)
尝试使用碳素
use Carbon\Carbon;
$startDate = Carbon::parse('05-07-2019');
$endDate = Carbon::parse('10-08-2021');
$diff = $startDate->diffInYears($endDate);
希望这会有所帮助:)
答案 1 :(得分:1)
尝试
$datetime1 = new DateTime("05-07-2019");
$datetime2 = new DateTime("10-08-2021");
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
输出将为
Difference: 2 years, 1 months, 5 days
希望这会有所帮助:)
答案 2 :(得分:0)
尝试使用date_diff,在您的示例中它将返回1。
$start_date = date_create("05-07-2019");
$end_date = date_create("10-08-2021");
$diff = date_diff($start_date, $end_date);
echo $diff->y;