"1 month" for the time calculation in PHP?

Source: Internet
Author: User
Tags echo date
Today, October 31, execute the code:

echo date('Ym', strtotime("-1 month"));

The returned result is 201410, is not the description similar to untrusted strtotime("-1 month") ?

Defines the time zone.

Php-v

PHP 5.4.33-1~dotdeb.0 (CLI) (built:sep 19 2014 12:00:21)
Copyright (c) 1997-2014 the PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

Reply content:

Today, October 31, execute the code:

echo date('Ym', strtotime("-1 month"));

The returned result is 201410, is not the description similar to untrusted strtotime("-1 month") ?

Defines the time zone.

Php-v

PHP 5.4.33-1~dotdeb.0 (CLI) (built:sep 19 2014 12:00:21)
Copyright (c) 1997-2014 the PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

case TIMELIB_MONTH:  s->time->relative.m += amount * relunit->multiplier; break;月份直接减1 ,当前日期 10月 31号 减一变成  9月31号 即是 10月1号

This is a PHP bug, you can specify the start time of the month.
$base = strtotime (Date (' Y-m ', Time ()). '-01 00:00:01 ');
echo Date (' m/y ', Strtotime ('-1 month ', $base));

Do you not define a time zone true real man? After defining the time zone my side is normal, PHP5.5 environment.

date_default_timezone_set("Asia/Shanghai");echo date("Y-m", strtotime("-1 month"));

May really like @ beautiful style is said to be a bug (read the manual does not know is not 5.2.7 of the bug), according to his definition of the relative time after the normal.

echo date("Y-m", strtotime("-1 month", strtotime("first day of this month")));
  • 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.