在日期计算中保持前导 0

WH

我想根据本月/年获得上个月/年,所以我使用的是 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.

如何保持领先008

或者,有没有更简单的方法来计算上个月?

阿卜杜拉希姆·苏拜-埃利德里西

试试这个 :

测试是否$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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章