zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

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

echodate("Y-m-dH: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");
/**
 *计算上一个月的今天
 *@paramtype$time
 *@returntype
 */
functionlast_month_today($time){
    $last_month_time=mktime(date("G",$time),date("i",$time),
               date("s",$time),date("n",$time),-1,date("Y",$time));
    returndate(date("Y-m",$last_month_time)."-dH:i:s",$time);
}
echolast_month_today($time);

但是此时又有了另一个问题,不存在2011-02-31这样的日期,怎么办?现在的需求是对于这样的日期显示当月最后一天。如下代码:
复制代码代码如下:
 $time=strtotime("2011-03-31");
/**
 *计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
 *@paramtype$time
 *@returntype
 */
functionlast_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<date("j",$time)){
       returndate("Y-m-tH:i:s",$last_month_time);
   }
   returndate(date("Y-m",$last_month_time)."-d",$time);
}
echolast_month_today($time);

这里需要注意一点:date(”Y-m”,$last_month_time).“-d”这段代码。在写代码的过程中如果写成了“Y-”.date(”m”,$last_month_time).“-d”则在跨年的时间上有问题。这点还是在写这篇文章时发现的。
除了这种方法,还可以先算出年月日再拼接字符串,这里就是纯粹的字符串操作了。