当前位置:Gxlcms > PHP教程 > php实现获取当前月与上个月月初及月末时间戳的方法

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

时间:2021-07-01 10:21:17 帮助过:14人阅读

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

具体如下:

当前月

<?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);//当前月的月末时间戳

上一月

<?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 MySQL 插入多条数据

Php多进程实现编程实例

如何通过PHP MySQL 插入数据

以上就是php实现获取当前月与上个月月初及月末时间戳的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行