我想根据本月/年获得上个月/年,所以我使用的是 php 代码/计算:
<?php
$this_month = date("m");
$this_year = date("Y");
if($this_month == "01"){
$history_month = "12";
$history_year = $this_year - 1;
}
else{
$history_month = $this_month - 1;
$history_year = $this_year;
}
$history_var = $history_year."".$history_month;
?>
问题是,当我回显时,history_var
我看到的20188
不是 201808
.
如何保持领先0
的08
?
或者,有没有更简单的方法来计算上个月?
试试这个 :
测试是否$history_month
小于 10 -> 添加零
<?php
$this_month = date("m");
$this_year = date("Y");
if($this_month == "01"){
$history_month = "12";
$history_year = $this_year - 1;
}
else{
$history_month = $this_month - 1;
$history_year = $this_year;
}
$history_month = $history_month >= 10 ? $history_month : "0".$history_month;
$history_var = $history_year."".$history_month;
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句