php - Carbon 将天数转换为人类可读的格式
问题描述
我需要将 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-interval):
默认因素是:
- 1 分钟 = 60 秒
- 1 小时 = 60 分钟
- 1 天 = 24 小时
- 1 周 = 7 天
- 1 个月 = 4 周
- 1 年 = 12 个月
CarbonIntervals 不携带上下文,因此它们不能更精确(没有 DST、没有闰年、没有真正的月份长度或年份长度考虑)。
推荐阅读
- reactjs - React.useEffect:在 axios 请求后状态未更新
- c# - 按自定义格式 DateTime 列对 DataTable 进行排序
- performance - 单个 QML Q_PROPERTY QString 消耗高 CPU (85%),每 90 毫秒发射一次
- elasticsearch - 有什么简单的方法可以通过logstash将数据推送到elasticsearch
- javascript - 解压 zip 文件并排除 NodeJS 中的根目录
- c# - C# 我的位图如何使用与指定的不同的 MemoryStream?
- python - 更改子目录中特定文件类型的行
- spring-boot - 无法使用 ConfigurationProperties 读取用户
- c++ - 函数中的自动变量在不同的调用中表示不同的类型是否合法?
- java - 用 Java 扫描服务器的网络流量