当前位置: 移动技术网 > IT编程>开发语言>PHP > php获取当前月与上个月月初及月末时间戳的方法

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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)转换工具:

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网