PHP's solution for eight hours of slow date ("Y-m-d H: I: s ")

Source: Internet
Author: User
Tags echo date local time
The date. timezone option has been added to PhP. ini since php5.1.0. It is disabled by default. Every time the database is inserted, it is found that the registration_date datetime is always eight hours slow. I tried to modify PHP. ini but it never succeeded. Later, the code was corrected. This is the IIS6 platform. It may be related to this.

Tested:

$ Timeoffset = 8;
Echo gmdate ("Y-m-d h: I: s", mktime () + $ timeoffset * 3600). "<br> ";

Echo gmdate ("Y-m-d h: I: s", time () + $ timeoffset * 3600). "<br> ";

Echo date ("Y-m-d h: I: s", mktime () + $ timeoffset * 3600). "<br> ";

Echo date ("Y-m-d h: I: s", time () + $ timeoffset * 3600). "<br> ";

All outputs:
2010-06-24 17:41:58
2010-106-24 17:41:58
2010-06-24 17:41:58
2010-06-24 17:41:58
It can be seen that the above methods are correct, but what is the difference? I have studied it again. The differences are as follows:
Date () and gmdate () are described in the official document as follows:

Date -- format a local time/date

Gmdate -- format a gmt/UTC date/time and return the Greenwich Mean Time (GMT ).
However, the above output results are the same because they were tested on the win platform. If they were running PHP on Linux + Apache, the results would be 8 hours less than they were.
Therefore, we should give a compatibility method, use gmdate in a unified manner, and manually set the current time zone. The improvement is as follows:

Echo gmdate ('Y-m-d H: I: s', time () + 3600*8 );

In this way, both Linux + Apache and Windows get the correct results. Of course, this write method also has the advantage that when the website is for the whole world, you only need to set the time zone where the Website user is located, the program automatically calculates the time based on the time zone set by the user. The database only saves the time generated by the current time (). The release time displayed in the China + 8 time zone is: 20010-06-14 12:15:27, then in Europe + 2 time zone users can see the release time of this information is: 17:41:58, so that all the information time is correct.
In addition, int time (void) returns the number of seconds from the Unix epoch (GMT 00:00:00, January 1, January 1, 1970) to the current time.
Mktime -- gets the Unix timestamp of a date and returns the Unix timestamp Based on the given parameter. The timestamp is a long integer that contains the number of seconds from the Unix epoch (January 1 1970 00:00:00 GMT) to the given time.
Int mktime ([int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst])
Parameters

Hour
Hours.

Minute
Minutes.

Second
Number of seconds (within one minute ).

Month
The number of months.

Day
Number of days.

Year
The number of copies per year, which can be two or four digits. 0-69 corresponds to 2000-2069,70-100 corresponds to 1970-2000. In today's systems, when time_t is generally used as a 32-bit signed integer, the valid range of year is between 1901 and 2038, but this restriction has been overcome since PHP 5.1.0.

Is_dst
This parameter can be set to 1, indicating that the system is in the daylight time (DST), 0 indicating that the system is not in the daylight time format, or-1 (default) indicating that the system is in the daylight time format. If it is unknown, PHP will try to understand it. This may produce unpredictable (but not incorrect) results. If DST or is_dst is enabled in the PHP running system and is set to 1, some time periods are invalid. For example, if DST takes effect from, all the time between and is invalid. mktime () returns an undefined (usually negative) value. For certain systems (such as Solaris 8), if DST takes effect at midnight, on the effective date of DST is calculated as on the previous day.

String date (string format [, int timestamp])

Returns the string generated by the integer timestamp according to the given format string. If no timestamp is provided, the current local time is used. In other words, timestamp is optional,The default value is time ().

Solution: 8-hour slow system time
Ini_set ("date. timezone", 'Asia/Shanghai'); // System Time Zone

Put the preceding statement into the system file to ensure that other files can be read to correct the time zone.

You can use the date command to modify Linux time.
On the command line, enter:

Date
Display the current time at 10:03:16, January 1, November 27 CST 2010
Date-S
Modified by string
You can modify only the date without changing the time. Enter: Date-s 20010-06-27.
Modify the time only. Enter date-s 10:03:00.
Modify the date and time at the same time. Note that double quotation marks are required. There is a space between the date and time. Enter date-s "10:03:00"

After modification, enter clock-w.
Write System Time to CMOS

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.