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$format * @return */ 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
##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 = '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
PHP implementationTimeUsage of addition and subtraction function strtotime
php How to set up a session that strictly controls the expirationtime
php How to gettime
##
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!