php - 如果数字大于 12,则使用 PHP 使数字从 1 开始
问题描述
我正在尝试用尼泊尔语转换英语日期。到目前为止,我已经完成了以下工作:
function nepaliYear($year){
$year = date('Y', strtotime($year)) + 56;
$month = date('m', strtotime($year)) + 8;
$days = date('d', strtotime($year)) + 15;
if($month > 12){
$year = $year + 1;
$month = date('H', strtotime($month)); //need help here
}
return $year.'-'.$month.'-'.$days;
}
如果月份大于 12,我希望数字从 1 继续。假设这个月是 7 月,即 7,如果我做 7+8,它会给出 15。如何将月份设为 03?我试图H
在date
函数中实现这一点,因为时间将在 12 小时内。
我希望你能理解我的问题。如果被问到,将准备好添加解释。谢谢你。
解决方案
您可以使用一个简单的解决方案。
if($month > 12){
$year = $year + 1;
$month = $month - 12;
}
既然您在评论部分询问了有关转换日期的问题,
你知道有闰年(二月有时有 29 天)。例如 7 月和 8 月有 31 天。您通常不能说“将 144 天转换为月”,因为每个月都不同。
例如:
假设您需要将 94 天转换为月份。
<?php
$start_date = new DateTime(date("Y/m/d"));
$end_date = new DateTime(date("Y/m/d",strtotime("+94")));
$date_diff = date_diff($start_date,$end_date);
echo "$date_diff->m months $dd->d days";
?>
因为start_date
您可以使用特定日期。也因为end_date
这是正确的方法,所以每个闰年和一切都被观察到!
推荐阅读
- php - 获取laravel中复制记录的自动增量id
- json - 将 JSON 对象分配给扩展的 TypeScript 类
- java - JAR 文件中的应用程序问题
- amazon-web-services - AWS Amplify iOS:查找用户是否登录的最可靠方法是什么
- javascript - 自定义字符串是的验证
- android - 在片段中实现 onClickListener
- python - 如何在 Python 中连接两个数据框?
- reactjs - 在 React 组件中本地操作 redux 状态的最佳实践
- firebase - 突发奇想我得到这个 Renderflex 错误并且无法解决它?
- java - 关于本次eclipse中动态web项目组件的问题:Web Content和java资源不可见