【问题标题】:In PHP what is the most efficent way to get the last day of the previous month, regardless of what day of the month in the current month you are?在 PHP 中,获取上个月最后一天的最有效方法是什么,无论您是本月的哪一天?
【发布时间】:2010-07-02 05:30:06
【问题描述】:

我需要能够将上个月的最后一天转换为日期变量。该脚本可以在当月的任何一天运行。即使上个月的最后一天是29、30、31等,也需要动态调整。

什么是最好的日子?

【问题讨论】:

标签: php datetime strtotime


【解决方案1】:

我将给出一个没有提到重复项的方法。 mktime 支持超出范围的值,因此如果您要求 2010 年 7 月 40 日,它会给您 2010 年 8 月 9 日;您可以将其反转并要求当月的第 0 天以获取前一天的最后一天:

$lastDayTimestamp = mktime(0, 0, 0, date('n'), 0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多