教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 获取指定月第一天和最后一天

php 获取指定月第一天和最后一天

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php 获取指定月第一天和最后一天等资源,欢迎您收藏本站,我们将为您提供最新的php 获取指定月第一天和最后一天资源
本文章给大家介绍在php 中利用date和strtotime函数获取用户给定时间的第一天或文章最后一天,有需要了解学习的朋友可进入参考。

借助于date和strtotime函数,可以轻松的获取本月、下月以及上月的第一天和最后一天,下面分别给出其实现。其中函数的参数date格式为yyyy-MM-dd。

1、给定一个日期,获取其本月的第一天和最后一天

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5769')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5769>

function getCurMonthFirstDay($date) {
    return date('Y-m-01', strtotime($date));
}

function getCurMonthLastDay($date) {
    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' 1 month -1 day'));
}

2、给定一个日期,获取其下月的第一天和最后一天

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8546')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8546>

function getNextMonthFirstDay($date) {
    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' 1 month'));
}

function getNextMonthLastDay($date) {
    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' 2 month -1 day'));
}

3、给定一个日期,获取其下月的第一天和最后一天

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7663')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7663>

function getPrevMonthFirstDay($date) {
    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 month'));
}

function getPrevMonthLastDay($date) {
    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' -1 day'));
}

其中strtotime函数参数" 1 month",php会根据具体月份来确定增加多少天,可能是28、29(2月)、30(小月)或 31(大月);某月的第一天 "-1 day" 自然就是上个月最后一天,php也会根据月来智能确定是28、29、30或31。


strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳


Report a bug 说明
int strtotime ( string $time [, int $now = time() ] )
本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。

您可能感兴趣的文章:
php取本周、本月的第一天与最后一天
php 获取本月第一天与最后一天的代码
php 获取指定月第一天和最后一天
php获取每月第一天与最后一天实例
php获取本周、本月第一天与最后一天的时间戳
PHP常用日期时间操作合集
php实现上月最后一天及某月最后一天的代码
PHP获取指定日期所在月的第一天和最后一天几个例子
php获取上个月的最后一天
php取一个月的第一天的代码

[关闭]
~ ~