Home > Backend Development > PHP Tutorial > How to get China time and United States time in PHP

How to get China time and United States time in PHP

墨辰丷
Release: 2023-03-27 21:10:01
Original
1577 people have browsed it

This article mainly introduces the method of obtaining Chinese time and American time in PHP, involving PHP time zone selection and date and time related operation skills. It is very simple and practical. Friends in need can refer to it.

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

##US Time Zone:

America/New_York Eastern United States

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

The above is the entire content of this article, I hope it will be helpful to everyone learning helps.


Related recommendations:

PHP implementationTimeUsage of addition and subtraction function strtotime

php How to set up a session that strictly controls the expiration time

php How to get time

##

The above is the detailed content of How to get China time and United States time in PHP. For more information, please follow other related articles on 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