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

2019-11-13,,,

本文实例讲述了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天。含有多少天,月底就是多少号。

PS:本站还提供了一个Unix时间戳转换工具,包含了各种常见语言针对时间戳的操作方法,提供给大家参考:

Unix时间戳(timestamp)转换工具:
http://tools.kunjuke.com/code/unixtime

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

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

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

下载本文的Word格式文档,以方便收藏与打印。