Home >Backend Development >PHP Tutorial >PHP gets China time and United States time

PHP gets China time and United States time

高洛峰
高洛峰Original
2017-02-25 16:33:352769browse

The example in this article describes how PHP obtains China time (Shanghai time zone time) and United States time. Share it with everyone for your reference, the details are as follows:

China time:

/**
 * 获取中国时间,即上海时区时间
 * @param <type> $format
 * @return <type>
 */
function getChinaTime($format = "Y-m-d H:i:s") {
  $timezone_out = date_default_timezone_get();
  date_default_timezone_set(&#39;Asia/Shanghai&#39;);
  $chinaTime = date($format);
  date_default_timezone_set($timezone_out);
  return $chinaTime;
}
echo getChinaTime();//输出当前时间,如:2017-02-23 11:50:50

United States time zone:

America/New_York United States East

encapsulates another method:

/**
 * 时间格式化
 * @param string $dateformat 时间格式
 * @param int $timestamp 时间戳
 * @param int $timeoffset 时区偏差
 * @return string
 */
function qgmdate($dateformat = &#39;Y-m-d H:i:s&#39;, $timestamp = &#39;&#39;, $timeoffset = 8) {
  if(empty($timestamp)) {
    $timestamp = time();
  }
  $result = gmdate($dateformat, $timestamp + $timeoffset * 3600);
  return $result;
}
//应用举例:获取美国时间
echo qgmdate(&#39;Y-m-d H:i:s&#39;, &#39;&#39;, -4);//输出美国时间,如:2017-02-22 23:51:17

For more articles related to PHP obtaining Chinese time and American time, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn