首页 > 解决方案 > 如果数字大于 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?我试图Hdate函数中实现这一点,因为时间将在 12 小时内。

我希望你能理解我的问题。如果被问到,将准备好添加解释。谢谢你。

标签: phpdatedate-conversion

解决方案


您可以使用一个简单的解决方案。

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这是正确的方法,所以每个闰年和一切都被观察到!


推荐阅读