【问题标题】:Current month Minus 1 month [duplicate]本月减去 1 个月 [重复]
【发布时间】:2025-12-15 21:45:01
【问题描述】:

我如何正确地减去当月的 1 个月?

$current_month1  = date('m'); 
$current_month  = $current_month1-1;
echo $current_month;

//current ouput
6
//desired output
06

【问题讨论】:

标签: php


【解决方案1】:

检查以下内容:

$now = new \DateTime("now");
$past = $now->modify("-1 month");

DateTime::modify docs

你也可以使用 DateInterval,docs 有例子。

【讨论】:

    【解决方案2】:

    为此,您可以将datestrtotime 结合使用。

    echo date('m', strtotime('last month')); // 06
    

    【讨论】:

      【解决方案3】:

      date 中的 m 运算符将为您提供:

      echo date('m', strtotime('now - 1 month'));
      

      06

      【讨论】: