Share the custom function of converting the U.S. time to Beijing time in php.

Source: Internet
Author: User
Tags time zones

Share the custom function of converting the U.S. time to Beijing time in php.

Since the email system recently used time conversion, it was necessary to convert the email time downloaded from Google gmail to Beijing time, so I wrote a time conversion function, we hope to enlighten all those who need it. We all know that the time difference between China and the US is 13 hours. The following code is directly used:

function datezhuanhuan($dateparams){$ccc=strtotime($dateparams);$date=date(‘Y-m-d');$bjtime=date(‘Y-m-d',$ccc);if($date==$bjtime){$sbjtime=date(‘Y-m-d H:i:s',$ccc);echo substr($sbjtime,11,5);}else{$sbjtime=date(‘Y-m-d H:i:s',$ccc);return substr($sbjtime,5,5);}}

Another 8-hour algorithm:

$bj_time = date ("Y-m-d H:i:s" , mktime(gmdate('H')+8,gmdate('i'),gmdate('s'),gmdate('m'),gmdate('d'),gmdate('Y')));

PHP custom function optional attribute

Give him a default value
Example: function check ($ aa, $ bb = 0 ){}
The latter is optional.

The space in the United States shows how the time was transferred back to Beijing.

If you have control over the server, adjust the server time.
Php I know
1. If the software you use can set the time zone, use the software. Such as DZ, phpWind, and vBulletin.

2. If your software cannot set the time zone, you can modify the. htaccess file of the space.

Open your. htaccess file and add the same text

Code:

SetEnv TZ location

Location is set according to the region you want. For all the time zones, you can find www.php.net/manual/en/timezones.php.

For example, if you want to set the time to Atlanta, the syntax is as follows:

Code:

SetEnv TZ America/Atlanta

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.