php 上一个月的开始和结束,php获取上一个月的开始与结束时间遇到的问题
改正之前:
$_lastMonthStart = date(‘Y-m-1 00:00:00‘, strtotime("-1 month"));
$_lastMonthEnd = date(‘Y-m-d H:i:s‘, strtotime(‘-1 sec‘, strtotime($_monthStart)));
$preMonthRange = [$_lastMonthStart, $_lastMonthEnd];
想要的結(jié)果:上一個(gè)月的開始和結(jié)束時(shí)間這一范圍。
出現(xiàn)的問題描述:5月31號(hào)獲取到的上一個(gè)時(shí)間居然是5月1到5月31!!!正確的應(yīng)該是4月1到4月30
問題原因分析:
問題出現(xiàn)在5.31這一天減去一個(gè)月不存在4.31,所以變成了5.1導(dǎo)致出錯(cuò)。
解決方法:
在上面定義一個(gè)’標(biāo)準(zhǔn)時(shí)間‘,即本月的1號(hào)。
改正之后:
$standardTime = date(‘Y-m-1‘);
$_lastMonthStart = date(‘Y-m-1 00:00:00‘, strtotime("-1 month", strtotime($standardTime)));
$_lastMonthEnd = date(‘Y-m-d H:i:s‘, strtotime(‘-1 sec‘, strtotime($_monthStart)));
$preMonthRange = [$_lastMonthStart, $_lastMonthEnd];
完美解決問題!
原文:https://www.cnblogs.com/liuqd001/p/9144413.html
總結(jié)
以上是生活随笔為你收集整理的php 上一个月的开始和结束,php获取上一个月的开始与结束时间遇到的问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php视频流传输,视频流传输协议RTP/
- 下一篇: 注册后自动登录 php,php – 自动