【问题标题】:penultimate day of month in phpphp中一个月的倒数第二天
【发布时间】:2026-02-04 19:50:02
【问题描述】:

到目前为止

strtotime('last day of this month');

给了我最后一天 (31),但我想要倒数第二天 (30),我已经尝试过:

strtotime('-1 day','last day of this month');

还有 strtotime('本月最后一天') - strtotime('1 day');和其他东西。始终导致(格式化为 date())1969 年 12 月 31 日。

我怎样才能做到这一点?

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    你快到了。 strtotime() 期望参数 2 为整数,以防从日期中减去时间跨度。所以你需要

    1. 使用strtotime('last day of this month')将一个月的最后一天转换为整数时间戳值
    2. 然后通过 strtotime('-1 day', $valueFromStep1) 减去一天

    因此做

    strtotime('-1 day',strtotime('last day of this month'))

    给出预期的答案

    这是与您提出的问题类似的问题:Subtract 1 day with PHP

    strtotime 参考: http://php.net/manual/en/function.strtotime.php

    【讨论】:

    • 如果您发现此答案有用。请随意接受它:) PHPing 快乐!
    • 好的。同时,我将尝试使这个答案对那里的人们更有用:)
    最近更新 更多