php教程|php手册
PHP,strtotime,php
php教程-php手册
PHP使用strtotime获取上个月、下个月、本月的日期,strtotime上个月
android商城淘宝源码下载,ubuntu没有输入指令,爬虫搜网软件,php shift,seo排名标题lzw
今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计当时太困了吧,再看到有种毛骨悚然的感觉,本来是想再处理一下的,但是一想肯定还有什么超方便的方法,于是找到了下面这个版本,做了一点小修改。
网站模板html源码下载,vscode两格缩进,短信仿真ubuntu,tomcat搭建测试环境,爬虫状熔岩,php 字符串格式化时间,沧县关键词seo优化价格,乌海中考分数线网站源码lzw
获取本月日期:
表白制作源码,ubuntu查看图标,tomcat数据库调优,tkinter爬虫实例,旅行社源码php,前端框架.seolzw
function getMonth($date){ $firstday = date("Y-m-01",strtotime($date)); $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day")); return array($firstday,$lastday); }
$firstday是月份的第一天,假如$date是-2这样的话,$firstday就会是-02-01,然后根据$firstday加一个月就是-03-01,再减一天就是-02-28,用date()和strtotime()真是太方便了。
获取上月日期:
function getlastMonthDays($date){ $timestamp=strtotime($date); $firstday=date(Y-m-01,strtotime(date(Y,$timestamp).-.(date(m,$timestamp)-1).-01)); $lastday=date(Y-m-d,strtotime("$firstday +1 month -1 day")); return array($firstday,$lastday); }
上月日期需要先获取一个时间戳,然后在月份上-1就OK了,超智能的date()会把-0-1这种东西转换成-12-01,太爽了。
获取下月日期:
function getNextMonthDays($date){ $timestamp=strtotime($date); $arr=getdate($timestamp); if($arr[mon] == 12){$year=$arr[year] +1;$month=$arr[mon] -11;$firstday=$year.-0.$month.-01;$lastday=date(Y-m-d,strtotime("$firstday +1 month -1 day")); }else{$firstday=date(Y-m-01,strtotime(date(Y,$timestamp).-.(date(m,$timestamp)+1).-01));$lastday=date(Y-m-d,strtotime("$firstday +1 month -1 day")); } return array($firstday,$lastday); }
下月日期的代码看起来比较长一点,因为date()转不了类似-13-01这种东西,它会直接回到1970,所以前面需要处理一下12月的问题,除了12月就直接月份+1就OK啦。
总得来说,还是很方便的,日期函数太强大了。
最后简单介绍下strtotime的用法
获取指定日期的unix时间戳
strtotime(“-1-22”) 示例如下:
echo strtotime(“-1-22”)
结果:1232553600
说明:返回1月22日0点0分0秒时间戳
获取英文文本日期时间
示例如下:
便于比较,使用date将当时间戳与指定时间戳转换成系统时间
(1)打印明天此时的时间戳strtotime(“+1 day”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“+1 day”))
结果:-01-23 09:40:25
(2)打印昨天此时的时间戳strtotime(“-1 day”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“-1 day”))
结果:-01-21 09:40:25
(3)打印下个星期此时的时间戳strtotime(“+1 week”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“+1 week”))
结果:-01-29 09:40:25
(4)打印上个星期此时的时间戳strtotime(“-1 week”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“-1 week”))
结果:-01-15 09:40:25
(5)打印指定下星期几的时间戳strtotime(“next Thursday”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“next Thursday”))
结果:-01-29 00:00:00
(6)打印指定上星期几的时间戳strtotime(“last Thursday”)
当前时间:
echo date(“Y-m-d H:i:s”,time())
结果:-01-22 09:40:25
指定时间:
echo date(“Y-m-d H:i:s”,strtotime(“last Thursday”))
结果:-01-15 00:00:00
以上示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间。
您可能感兴趣的文章:
PHP strtotime函数详解php 深入理解strtotime函数的使用详解PHP时间戳 strtotime()使用方法和技巧php使用strtotime和date函数判断日期是否有效代码分享PHP下获取上个月、下个月、本月的日期(strtotime,date)php使用date和strtotime函数输出指定日期的方法
如果觉得《PHP使用strtotime获取上个月 下个月 本月的日期 strtotime上个月》对你有帮助,请点赞、收藏,并留下你的观点哦!