PHP gets China time and United States time

高洛峰
Release: 2023-03-06 07:16:02
Original
2730 people have browsed it

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
Copy after login

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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!