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

yipeiwu_com5年前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天。含有多少天,月底就是多少号。

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

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

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

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

相关文章

php array_intersect()函数使用代码

array array_intersect ( array array1, array array2 [, array ...]) array_intersect() 函数返回两个或多个...

php设计模式 Adapter(适配器模式)

复制代码 代码如下: <?php /** * 适配器模式 * * 将一个类的接口转换成客户希望的另外一个接口,使用原本不兼容的而不能在一起工作的那些类可以在一起工作 */ // 这...

PHP提示Cannot modify header information - headers already sent by解决方法

本文实例讲述了PHP提示Cannot modify header information - headers already sent by解决方法,是进行PHP程序设计过程中经常会遇到...

PHP队列原理及基于队列的写文件案例

PHP队列原理及基于队列的写文件案例

本文实例讲述了PHP队列原理及基于队列的写文件案例。分享给大家供大家参考,具体如下: 队列是一种线性表,按照先进先出的原则进行的: 入队: 出队: PHP实现队列:第一个元素作为队头...

推荐一款MAC OS X 下php集成开发环境mamp

推荐一款MAC OS X 下php集成开发环境mamp

之前苦于mac上搭建本地服务器之艰辛,找寻好久都没找到一款类似windows上集成的本地服务器环境,诸如phpstudy,xampp,appserv,虽说xampp也有mac版,但不知为...