您的位置:首页 > 编程语言 > PHP开发

PHP怎么得出上个月份的,本月的开始时间和结束时间

2017-09-28 14:58 323 查看

1. 事情是这样婶儿的

早晨来了,主管让我写个脚本,定时删除上个月的logs日志文件,数据库中存入了每条记录的请求时间,我只要找到月份的开始时间和结束时间就行了


2. 代码

<?php
//上当前月份
$month = date("m", strtotime("last month"));
//找到上个月份的开始时间戳和结束时间戳
$days = date("t", strtotime("last month"));
//mktime(hour,minute,second,month,day,year,is_dst);
$lastmonth_year = date("Y");
if ($month == 1) {
$year = date("Y");
$lastmonth_year = $year - 1;
}
//上个月开始的时间戳
$begin = mktime(0, 0, 0, $month, 1, $lastmonth_year);
//上个月结束的时间戳
$end = mktime(23, 59, 59, $month, $days, $lastmonth_year);
//获取本月的开始时间和结束时间
$month = date("Y-m");
$month_begin_date = $month . "-01";
$month_begin = strtotime($month_begin_date);
$next = strtotime("next month");
$next_month_begin_date = date("Y-m",$next) . "-01";
$month_end = strtotime($next_month_begin_date) - 1;
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 脚本
相关文章推荐