【问题标题】:Get next month using mktime()使用 mktime() 获取下个月
【发布时间】:2015-06-23 11:54:57
【问题描述】:

我正在尝试使用 PHP mktime() 函数获得下个月。

我希望得到类似07/2015 的结果,但它却给了我01/2015

这是我正在使用的代码:

$next_month = strftime('%d/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));

$month 的值为 06。

$year 的值为 2015。

【问题讨论】:

  • 当然你会收到01/2015。在您专门将其设置为1 之后,您将打印一个月中的哪一天。我怀疑你想要strftime('%m/%Y', ...);

标签: php date mktime


【解决方案1】:

如果你坚持使用你的版本,那么它应该是%m而不是%d,即:

$year = 2015;
$month = 6;
echo strftime('%m/%Y', strtotime('+1 month', mktime(0,0,0,$month,1,$year)));

【讨论】:

    【解决方案2】:

    试试DateTime 怎么样。例如:

    $date = new DateTime('next month');
    echo $date->format('m/Y');
    

    如果月份和年份是可变的,那么以下方法也可以:

    $date = new DateTime("$year-$month-01");
    $date->modify('next month');
    echo $date->format('m/Y');
    

    【讨论】:

    • 如何使用 06/2015 的输入?我基本上需要在 06/2015 上增加 1 个月。
    • 更新了变量输入。
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多