Key Points of PHP Time Processing

Source: Internet
Author: User
Tags time zones

The world is divided into 24 time zones, GMT is a zero time zone, that is, the Greenwich Mean Time. China is located in GMT, eight hours more than GMT. For example, GMT is 09:45:32, and our time is 01:45:32.

The mktime () function of PHP returns the GMT time, and the date () function returns the time corresponding to the time zone set by the date. timezone or date_default_timezone_set () function.

The available time zones of PHP in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (Chongqing, Shanghai, and Urumqi in sequence), and Hong Kong and Taiwan regions are available: Asia/Macao, asia/hong_kong, Asia/Taipei (in the order of Macao, Hong Kong, and Taipei), and Singapore: Asia/Singapore.

You can use the gmdate () function to convert the timestamp into a string in PHP:

 
$ S=Gmdate('Y-m-d h: I: s', 1308279886 );

 

The common practice of PHP processing time is to use an integer in the MySQL database to save the timestamp, and increase or decrease the corresponding time zone difference during frontend processing.

MySQL can use from_unixitme () to convert the timestamp to the datetime type. This function will be affected by the time zone:

Set session time_zone = '+ 0: 00'; Select updatetime, From_unixtime (updatetime) from tbs_articles;

The above SQL statement returns the GMT time because the time zone is set to zero:

1308279886 03:04:46
If set session time_zone = '+ 8:00' is executed, the returned result is:
1308279886 11:04:46

MySQL can use the date_format function to format the datetime type:

 
Set session time_zone = '+ 0: 00'; Select updatetime, From_unixtime (updatetime), date_format (from_unixtime (updatetime), '% Y-% m-% d % H: % I: % s') From tbs_articles; select updatetime, From_unixtime (updatetime), date_format (from_unixtime (updatetime + 8*3600), '% Y-% m-% d % H: % I: % s') from tbs_articles;

 

Refer:
1. World Time Zone/TimeHttp://app.travel.ifeng.com/tools/timezone-index-3.html
2. World Time Zone DiagramHttp://www.24-hotel.com.cn/w_hotel/world_time.asp
3. List of time zones supported by PHPHttp://php.net/manual/zh/timezones.php
4. MySQL Date and Time FunctionsHttp://www.phpchina.com/resource/manual/mysql/functions.html#date-and-time-functions
5. php date and formatHttp://cn.php.net/manual/zh/function.date.php

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.