PHP Strtotime calculates the last one months of today's problem solving _php tutorial

Source: Internet
Author: User
Tags echo date
Today's small series to introduce you to a PHP strtotime calculation on the one months of today's problem resolution, if you hit the calculation for one months today there is a problem not to enter the reference.

PHP, last one months

Strtotime has a little problem.

The code is as follows Copy Code

> php-r "echo Date (' Ymd000000′,strtotime ('-1 month ', Strtotime (' 201307310000′)");
20130701000000#
> php-r "echo Date (' Ymd000000′,strtotime ('-1 month ', Strtotime (' 201308010000′)");
20130701000000#

Search for a bit, the following method is more accurate

The code is as follows Copy Code

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


The function mktime, which can get the timestamp of the date:

int mktime ([int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year [, int $is _DST]] []]

Returns the Unix timestamp based on the given parameters. The timestamp is a long integer containing the number of seconds from the Unix era (January 1 1970 00:00:00 GMT) to a given time.

Parameters can be omitted from right to left, and any omitted parameters are set to the current value of the cost date and time.
So it can be used to calculate

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 result of the output is 2012-03-02; it should be the output 2012-02-31???? Idiot, did you have number 31st in February? No. 30th, no? Is there a number 29th??.. This one.. can have ...
PHP has dealt with this situation for us, and he will do it for a few more days by next month.
February 2012 The last day is 2012-02-29 then 31st is more than 29 two days, so PHP added to the next month to deal with is 2012-03-02.
Almost forgot. How about the day of the week?? Don't count it. PHP is ready for us.
Date (' W ', $strtime); The first day of the week.

Example

copy code

/**
* Calculated for the previous one months today, if not today, then return to the last day of one months
* @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);

http://www.bkjia.com/PHPjc/633186.html www.bkjia.com true http://www.bkjia.com/PHPjc/633186.html techarticle today's small series to introduce you to a PHP strtotime calculation on the one months of today's problem resolution, if you hit the calculation for one months today there is a problem not to enter the reference. PHP, last one months ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.