服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - php获取当前月与上个月月初及月末时间戳的方法

php获取当前月与上个月月初及月末时间戳的方法

2021-04-02 19:55牛逼的霍啸林 PHP教程

这篇文章主要介绍了php获取当前月与上个月月初及月末时间戳的方法,涉及php针对日期与时间相关判断与操作技巧,需要的朋友可以参考下

本文实例讲述了php获取当前月与上个月月初及月末时间戳的方法。分享给大家供大家参考,具体如下:

当前月

?
1
2
3
4
5
6
7
<?php
$thismonth = date('m');
$thisyear = date('Y');
$startDay = $thisyear . '-' . $thismonth . '-1';
$endDay = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay));
$b_time  = strtotime($startDay);//当前月的月初时间戳
$e_time  = strtotime($endDay);//当前月的月末时间戳

上一月

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$thismonth = date('m');
$thisyear = date('Y');
if ($thismonth == 1) {
 $lastmonth = 12;
 $lastyear = $thisyear - 1;
} else {
 $lastmonth = $thismonth - 1;
 $lastyear = $thisyear;
}
$lastStartDay = $lastyear . '-' . $lastmonth . '-1';
$lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay));
$b_time = strtotime($lastStartDay);//上个月的月初时间戳
$e_time = strtotime($lastEndDay);//上个月的月末时间戳

这里对关键的就是date函数中的t,它是用来获取当前月所含天数的,28天,29天,30天,31天。含有多少天,月底就是多少号。

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐