What is the difference between date and gmdate in PHP? _php Tutorials

Source: Internet
Author: User
Tags echo date time zones set time

What is the difference between date and gmdate in PHP?


What is the difference between date and gmdate in PHP?

The time in PHP has 2 formatting functions: Date () and Gmdate (), which are described in the official documentation:

date-formatting a local time/date

gmdate-formats a GMT/UTC date/time and returns the Greenwich Mean Time (GMT).

For example, we are now in the time zone is +8, then the server run the following script to return the time should be this:

The current time is assumed to be 2007-03-14 12:15:27

echo Date (' y-m-d h:i:s ', Time ()); Output is: 2007-03-14 12:15:27

echo gmdate (' y-m-d h:i:s ', Time ()); Output is: 2007-03-14 04:15:27

But this is only the result of running PHP under Linux+apache, if run under Windows, then 2 functions returned are: 2007-03-14 04:15:27.

Therefore, we should give a compatibility of the wording, unified use of gmdate, and manually set the current time zone, the following improvements:

echo gmdate (' y-m-d h:i:s ', time () + 3600 * 8);

So no matter under the Linux+apache or windows are getting the correct results, of course, there is a benefit to write, when the site is facing the world, then the site users as long as the settings in the time zone, the program automatically based on the time zone set by the user to calculate, The publication time of the information in the database is only the time that is generated by the current date (), then the published time in China + 8 time zone is: 2007-03-14 12:15:27, then in Europe + 2 time zone users see this information published: 2007-03-14 06:15:27, the time for this information is all right.

Second, modify the default time zone of PHP affect them

Each region has its own local time, and in the Internet and radio communication, time conversion problem is particularly prominent. The entire Earth is divided into 24 time zones, each with its own local time. In international radio or network communications, for the sake of unification, the use of a unified time, known as universal coordination (UTC,UNIVERSAL-time coordinated), is a global standard time set by the world-standard. UTC was also known as Greenwich Mean Time (Gmt,greenwich Mean times), which is the same as the local time in London, UK.

The default time zone setting for PHP is UTC, and Beijing is located in the East eight zone of the time zone, leading UTC eight hours. So when you use PHP like Time () and so on to get the current times of the function, the resulting time is always wrong, the performance is eight hours with Beijing time difference. If you want to display Beijing time correctly, you will need to modify the default time zone settings, which can be done in the following two ways.

If you are using a standalone server with permissions to modify the configuration file, setting the time zone can be done by modifying the Date.timezone property in PHP.ini. We can set the value of this property to one of "Asia/shang", "asia/chongqing", "Etc/gmt-8″ or PRC," and then the current time in PHP script is Beijing time. The configuration file for modifying PHP is as follows:

Date.timezone = etc/gmt-8

Set the default time zone in the configuration file to East 8 (Beijing time)

If you are using a shared server, you do not have permission to modify the configuration file php.ini, and the PHP version is at 5.1.0 or more, you can also call the Date_default_timezone_set () function to set the time zone before the output time. The function needs to provide a time zone identifier as a parameter, and the value of the Date.timezone property in the configuration file is the same. The use of this function is as follows:

Date_default_timezone_set (' PRC ');

Set time zone before output time, PRC for the People's Republic of China Echo date (' y-m-d h:i:s ', Times ());

The current time for the output is GMT

Third, test:

1, the code is as follows:

  

Date_default_timezone_set (' etc/gmt ');

Set the time zone before the output time, ETC/GMT is the standard Time

Echo (Time ());

Echo ('
’);

echo Date (' y-m-d h:i:s ', Time ());

Echo ('
’);

Echo gmdate ("y-m-d h:i:s", Time ());

Echo ('
’);

Date_default_timezone_set (' PRC ');

Set time zone before output time, PRC for PRC

Echo (Time ());

Echo ('
’);

echo Date (' y-m-d h:i:s ', Time ());

Echo ('
’);

Echo gmdate ("y-m-d h:i:s", Time ());

?>

Copy Code

2. Results:

1276257131

2010-06-11 11:52:11

2010-06-11 11:52:11

1276257131

2010-06-11 19:52:11

2010-06-11 11:52:11

3. Conclusion:

(1) Time (): Standard timestamp

Returns the number of seconds since the Unix era (GMT January 1, 1970 00:00:00) to the current time (referring to the current GMT Standard Time), and its value is independent of the time zone set by the PHP system.

Date (): Returns the "custom format" time of the localized time for the current GMT standard Time, which is related to the time zone set by the PHP system.

Gmdate (): Returns the "custom format" time for the current GMT standard time, regardless of the time zone set by the PHP system.

(2) Unix timestamp: Refers to the number of seconds that the current (GMT standard) time is away from the Unix era (1970-01-01 00:00:00);

Code:

  

Date_default_timezone_set (' etc/gmt ');

Set the time zone before the output time, ETC/GMT is the standard Time

Echo (Strtotime (' 1970-01-01 08:00:00′));

Echo ('
’);

Date_default_timezone_set (' PRC ');

Set time zone before output time, PRC for PRC

Echo (Strtotime (' 1970-01-01 08:00:00′));

?>

Copy Code

Test results:

28800

0

Strtotime (a): The UNIX timestamp of GMT Standard time for the current time, if the time zone set by the current PHP system is not the GMT Standard Time zone, then when the Strtotime (a) is applied, the system automatically translates it into the corresponding GMT standard. It then calculates the Unix timestamp for this time. Associated with the time zone set by the PHP system

http://www.bkjia.com/PHPjc/881293.html www.bkjia.com true http://www.bkjia.com/PHPjc/881293.html techarticle What is the difference between date and gmdate in PHP? What is the difference between date and gmdate in PHP? The time in PHP has 2 formatting functions: Date () and Gmdate (), described in the official document as: Dat ...

  • 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.