如何使用PHP计算上一个月的今天
时间:2023-11-15 14:16:25
一日,遇到一个问题,求上一个月的今天。 最开始我们使用 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 < date("j", $time)) {
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” 则在跨年的时间上有问题。 这点还是在写这篇文章时发现的。
除了这种方法,还可以先算出年月日再拼接字符串,这里就是纯粹的字符串操作了。
标签:php,计算
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue踩坑日记之params传递参数问题
2024-05-10 14:19:48
![](https://img.aspxhome.com/file/2023/7/126407_0s.png)
详解Python3 对象组合zip()和回退方式*zip
2021-03-01 11:14:09
![](https://img.aspxhome.com/file/2023/2/115432_0s.webp)
python重试装饰器的简单实现方法
2022-07-15 12:54:10
PyCharm配置anaconda环境的步骤详解
2023-07-26 10:11:20
![](https://img.aspxhome.com/file/2023/1/105561_0s.jpg)
VUE中如何动态绑定类名和样式
2024-04-26 17:41:35
numpy创建单位矩阵和对角矩阵的实例
2023-08-05 10:25:44
详解Python中的四种队列
2021-05-10 01:48:04
![](https://img.aspxhome.com/file/2023/4/124894_0s.png)
kafka监控获取指定topic的消息总量示例
2023-09-04 01:44:48
Python自定义一个异常类的方法
2022-04-23 12:51:11
![](https://img.aspxhome.com/file/2023/2/130752_0s.jpg)
Python+matplotlib实现华丽的文本框演示代码
2021-07-19 08:19:51
![](https://img.aspxhome.com/file/2023/1/92531_0s.png)
解决Pycharm无法import自己安装的第三方module问题
2023-09-02 20:25:53
![](https://img.aspxhome.com/file/2023/5/89325_0s.jpg)
python批量读取文件名并写入txt文件中
2021-02-18 16:32:29
python3中pip3安装出错,找不到SSL的解决方式
2022-02-15 23:33:17
JavaScript 数组中最大最小值
2024-04-27 15:23:56
JavaScript实现切换多张图片
2024-04-17 09:54:18
![](https://img.aspxhome.com/file/2023/9/136469_0s.jpg)
关于python列表相关知识点
2021-07-25 06:54:05
![](https://img.aspxhome.com/file/2023/1/134791_0s.png)
python网络应用开发知识点浅析
2021-07-15 00:09:48
asp sql数据验证之特殊字符处理
2008-02-24 16:35:00
python中如何使用朴素贝叶斯算法
2023-02-26 08:51:39
![](https://img.aspxhome.com/file/2023/5/95795_0s.png)
Express无法通过req.body获取请求传递的数据解决方法
2024-06-05 09:52:06
![](https://img.aspxhome.com/file/2023/8/122388_0s.png)