PHP strtotime calculation of today's problem in the previous month - PHP tutorial

WBOY
Release: 2016-07-13 10:43:23
Original
968 people have browsed it

Today, the editor will introduce to you a solution to today's problem in calculating PHP strtotime for the previous month. If you encounter problems calculating today for the previous month, please refer to it for reference.

PHP, previous month

strtotime has a small problem

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

> php -r”echo date(‘Ymd000000′,strtotime ( ‘-1 month’, strtotime ( ’201307310000′ ) ));”
20130701000000#
> php -r”echo date(‘Ymd000000′,strtotime ( ‘-1 month’, strtotime ( ’201308010000′ ) ));”
20130701000000#

Copy code

 代码如下 复制代码

$time = strtotime("2011-03-31");

> php -r”echo date(‘Ymd000000′,strtotime (‘-1 month’, strtotime (’201307310000′)));”

20130701000000#
> php -r”echo date(‘Ymd000000′,strtotime (‘-1 month’, strtotime (’201308010000′)));”

20130701000000#

After searching, the following method is more accurate

Returns a Unix timestamp based on the given arguments. A timestamp is a long integer containing the number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) to a given time.
The code is as follows Copy code
 代码如下 复制代码

function last_month_day($time){
    $strtime=mktime(date('h',$time),date('i',$time),date('s',$time),date('m',$time)-1,date('d',$time),date('Y',$time));
    echo date('Y-m-d',$strtime);
}
 last_month_day(strtotime("2012-03-31"));


$time = strtotime("2011-03-31");



Function mktime, which can get the timestamp of the date:

 代码如下 复制代码

/**
 * 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
 * @param type $time
 * @return type
 */
function last_month_today($time){
    $last_month_time = mktime(date("G", $time), date("i", $time),
                date("s", $time), date("n", $time), 0, date("Y", $time));
    $last_month_t =  date("t", $last_month_time);

    if ($last_month_t < date("j", $time)) {
        return date("Y-m-t H:i:s", $last_month_time);
    }

    return date(date("Y-m", $last_month_time) . "-d", $time);
}

echo last_month_today($time);

int mktime ([ int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is_dst ]]]]]] )
Parameters can be omitted from right to left. Any omitted parameters will be set to the current value of the local date and time. So it can be calculated
The code is as follows Copy code
function last_month_day($time){ $strtime=mktime(date('h',$time),date('i',$time),date('s',$time),date('m',$time)-1,date(' d',$time),date('Y',$time)); echo date('Y-m-d',$strtime); } last_month_day(strtotime("2012-03-31")); The output result is 2012-03-02; logically it should be the output 2012-02-31???? Idiot, is there a 31st in February? No, is there a 30th, no? Is there a 29th?... This. .Can have… PHP handles this situation for us, and it will give us a few more days to calculate next month. The last day of February 2012 is 2012-02-29, so the 31st is two days longer than the 29th, so PHP will add it up to the next month for processing, which is 2012-03-02. I almost forgot... So how do we calculate the day of the week? No need to calculate it... PHP has prepared it for us. date(‘w’,$strtime); The output is the day of the week. It is the day of the week. Example
The code is as follows Copy code
/** * Calculate today's day of the previous month. If there is no today in the previous month, return the last day of the previous month * @param type $time * @return type ​*/ function last_month_today($time){ $last_month_time = mktime(date("G", $time), date("i", $time), date("s", $time), date("n", $time), 0, date("Y", $time)); $last_month_t = date("t", $last_month_time); if ($last_month_t < date("j", $time)) {          return date("Y-m-t H:i:s", $last_month_time); } return date(date("Y-m", $last_month_time) . "-d", $time); } echo last_month_today($time);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633186.htmlTechArticleToday the editor will introduce to you a solution to the problem of calculating today's date in the previous month with PHP strtotime. If you encounter If there is any problem in calculating the previous month today, please do not enter the reference. PHP, last month...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template