Home  >  Article  >  php教程  >  如何使用PHP计算上一个月的今天

如何使用PHP计算上一个月的今天

WBOY
WBOYOriginal
2016-06-13 11:52:241013browse

一日,遇到一个问题,求上一个月的今天。 最开始我们使用 strtotime(”-1 month”) 函数求值,发现有一个问题,月长度不一样的月份的计算结果有误。 比如:2011-03-31,得到的结果是2011-03-03。我们先不追究什么问题,先看如何解决问题。 此时,想起PHP中有一个mktime函数,于是自己写了如下代码:

复制代码 代码如下:


echo date("Y-m-d H:i:s", mktime(date("G", $time), date("i", $time),
 date("s", $time), date("n", $time) - 1, date("j", $time), date("Y", $time)));


当执行时,发现结果和strtotime的结果是一样的。
还是基于这个函数,既然无法直接操作月,那么我们从天入手,得到上一个月,然后再使用date拼接数据。如下代码:

复制代码 代码如下:


$time = strtotime("2011-03-31");
/**
 * 计算上一个月的今天
 * @param type $time
 * @return type
 */
function last_month_today($time) {
     $last_month_time = mktime(date("G", $time), date("i", $time),
                date("s", $time), date("n", $time), - 1, date("Y", $time));
     return date(date("Y-m", $last_month_time) . "-d H:i:s", $time);
}
echo last_month_today($time);


但是此时又有了另一个问题,不存在2011-02-31这样的日期,怎么办?现在的需求是对于这样的日期显示当月最后一天。 如下代码:

复制代码 代码如下:


 $time = strtotime("2011-03-31");
/**
 * 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
 * @param type $time
 * @return type
 */
function last_month_today($time){
    $last_month_time = mktime(date("G", $time), date("i", $time),
                date("s", $time), date("n", $time), 0, date("Y", $time));
    $last_month_t =  date("t", $last_month_time);
    if ($last_month_t         return date("Y-m-t H:i:s", $last_month_time);
    }
    return date(date("Y-m", $last_month_time) . "-d", $time);
}
echo last_month_today($time);


这里需要注意一点: date(”Y-m”, $last_month_time) . “-d”这段代码。在写代码的过程中如果写成了 “Y-” . date(”m”, $last_month_time) . “-d” 则在跨年的时间上有问题。 这点还是在写这篇文章时发现的。
除了这种方法,还可以先算出年月日再拼接字符串,这里就是纯粹的字符串操作了。
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn