Summarize the problem of PHP time processing

Source: Internet
Author: User
Tags echo date ini what php

The problem is to deal with a time

echo Date (' Ymd ', Strtotime (' +1 month ', Strtotime (' 20150130 '));

According to the friend's idea, should output is 20150227, and the actual output is 20150302
That means that the January 29,30,31 output is the number of March days, not the number of days to get the February.
As I understand it, the month + 1 months is the number of days to add the month, the idea is also validated.
Cases

echo Date (' Ymd ', Strtotime (' +1 month ', Strtotime (' 20150331 '));

I want 20150430 results and I get 20150501.
So according to this kind of writing in the statistical time will appear the error. Of course, I haven't read the program he wrote,
I do not understand why it was added one months at the end of the month.
The solution I gave him was to start computing again every month. So there's no mistake.
Finally he gave the ultimate settlement bill is

echo Date (' Ym ', Strtotime (' Day of next month ', Strtotime (' 20150130 '));

To prove the same thing as I thought.

His stubborn pig must prove that PHP is not perfect, but also specifically to find Ruby solution.
According to her request, I posted the Ruby code he was looking for.

Date.new (2015,1,30) +

The final result of Ruby is the February 27 of Hope.

PHP 8-hour time lag problem cause

Starting with php5.1.0, PHP.ini added the Date.timezone option, which is closed by default
That is, the time that is displayed (no matter what PHP command is used) is Greenwich Standard Time
And our Time (Beijing time) is just 8 hours bad.

The following 3 methods are available to restore normal time.

1, the simplest way is not to use the php5.1 above version
2, if you must use, and can not modify php.ini, you need in the initialization of the statement of time
Add Date_default_timezone_set (' XXX ') above;

3, once and for all, can only modify php.ini. Open php.ini Find Date.timezone remove the preceding semicolon
Date.timezone = PRC, restart the HTTP service (such as Apache2 or IIS, etc.)
"Related notes:" ↓
For XXX, the values available in mainland China are: asia/chongqing, Asia/shanghai, Asia/urumqi (Chongqing, Shanghai, Urumqi)
Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei)
and Singapore: Asia/singapore
The foreigner seems to have leaked Beijing

Other available values are: etc/gmt-8, Singapore, Hongkong, PRC
What is PRC? PRC is the People's Republic of China Ah-_-
You can find more areas on the http://www.php.net/docs.php.
The program will automatically use Greenwich Mean time when you are not using a built-in area, because it will eventually be converted to a time zone.

Please note

If you do not have permission to modify the php.ini, you should add Date_default_timezone_set (' PRC ') before calling the date () method;
Argument with double or single quotation marks

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.