Home  >  Article  >  Backend Development  >  php Get the first and last day of a specified month_PHP tutorial

php Get the first and last day of a specified month_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:57:291150browse

This article will introduce to you how to use date and strtotime functions in PHP to obtain the first day of the user-given time or the last day of the article. Friends who need to learn more can enter for reference.

With the help of date and strtotime functions, you can easily obtain the first and last days of this month, next month and previous month. The implementations are given below. The parameter date format of the function is yyyy-MM-dd.

1. Given a date, get the first and last day of this month

The code is as follows
 代码如下 复制代码

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'));
}

Copy code

 代码如下 复制代码

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'));
}

function getCurMonthFirstDay($date) {

Return date('Y-m-01', strtotime($date));

}
 代码如下 复制代码

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'));
}

function getCurMonthLastDay($date) {

Return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'));

}



2. Given a date, get the first and last day of the next month

The code is as follows Copy code
function getNextMonthFirstDay($date) { Return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month'));

}

Return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +2 month -1 day')); } 3. Given a date, get the first and last day of the next month
The code is as follows Copy code
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')); } The strtotime function parameter "+1 month", PHP will determine how many days to add according to the specific month, which may be 28, 29 (February), 30 (small month) or 31 (big month); the first day of a certain month One day "-1 day" is naturally the last day of the last month. PHP will also intelligently determine whether it is 28, 29, 30 or 31 according to the month. strtotime — Parse any English text datetime description into a Unix timestamp Report a bug Description int strtotime ( string $time [, int $now = time() ] ) This function expects a string containing a date in US English format and attempts to parse it into a Unix timestamp (number of seconds since January 1 1970 00:00:00 GMT) relative to the time given by the now parameter, If this parameter is not provided, the current system time is used. http://www.bkjia.com/PHPjc/631520.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631520.htmlTechArticleThis article introduces how to use date and strtotime functions in php to get the first day or article of a given time by the user On the last day, friends who need to learn more can enter for reference. With the help of d...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn