php获取当前月与上个月月初及月末时间戳的方法
这里有新鲜出炉的 PHP 教程,程序狗速度看过来!
PHP 开源脚本语言
PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源脚本语言。语法吸收了 C 语言、Java 和 Perl 的特点,入门门槛较低,易于学习,使用广泛,主要适用于 web 开发领域。PHP 的文件后缀名为 php。
这篇文章主要介绍了 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);//当前月的月末时间戳
上一月
$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 时间戳转换工具,包含了各种常见语言针对时间戳的操作方法,提供给大家参考:
希望本文所述对大家 PHP 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0803/340939.html