Home >Backend Development >PHP Tutorial >PHP gets China time and United States time
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('Asia/Shanghai'); $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 = 'Y-m-d H:i:s', $timestamp = '', $timeoffset = 8) { if(empty($timestamp)) { $timestamp = time(); } $result = gmdate($dateformat, $timestamp + $timeoffset * 3600); return $result; } //应用举例:获取美国时间 echo qgmdate('Y-m-d H:i:s', '', -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!