碳将天数转换为人类可读的格式



我需要将30天转换为1个月。如果月和天是平均数,那么像1年2个月2天我在下面尝试过,但它会返回错误的结果

echo CarbonInterval::days(30)->cascade()->forHumans();

有人能帮助我如何做到这一点吗?

我尝试了以下解决方案,但只得到了2天的差异

$convert = '30'; // days you want to convert
$years = ($convert / 365) ; // days / 365 days
$years = floor($years); // Remove all decimals
$month = ($convert % 365) / 30.5; // I choose 30.5 for Month (30,31) ;)
$month = floor($month); // Remove all decimals
$days = ($convert % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$convert.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';

使用碳有什么好的解决方案吗

它必须是CarbonInterval吗?

Carbon::now()->subDays(1827)->diffForHumans()呢?

它没有像你预期的那样工作的原因(来自https://carbon.nesbot.com/docs/#api-间隔(:

默认因素为:

  • 1分钟=60秒
  • 1小时=60分钟
  • 1天=24小时
  • 1周=7天
  • 1个月=4周
  • 1年=12个月

CarbonIntervals不包含上下文,因此不能更精确(无夏令时,无闰年,无实际月长或年长考虑(。

最新更新