PHP获取中国时间(上海时区时间)及美国时间的方法

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP获取中国时间(上海时区时间)及美国时间的方法。分享给大家供大家参考,具体如下:

中国时间:

/**
 * 获取中国时间,即上海时区时间
 * @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

美国时区:

America/New_York 美国东部

封装了另外一个方法:

/**
 * 时间格式化
 * @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

PS:这里再为大家提供2款时间相关工具,供大家参考使用:

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

在线世界各地时间查询:
http://tools.jb51.net/zhuanhuanqi/worldtime

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

php获取本地图片文件并生成xml文件输出具体思路

复制代码 代码如下: <?php $dir="upload/"; $dir_res=opendir($dir); $fileFormat=array(0=>".jpg",1=...

php 获得汉字拼音首字母的函数

php获取汉字拼音的第一个字母复制代码 代码如下:<?php function getinitial($str) { $asc=ord(substr($str,0,1)); if...

解析php中获取系统信息的方法

$root = getenv('DOCUMENT_ROOT'); ////服务器文档根目录$port = getenv('SERVER_PORT'); ////服务器端口$file =...

php多重接口的实现方法

本文实例讲述了php多重接口的实现方法。分享给大家供大家参考。具体如下: <?php interface staff_i1 //接口1 { func...

PHP中Notice错误常见解决方法

对于初学者,肯定会遇到不同的错误提示,比如:警告,致命,等等,其中NOTICE错误等级最低,页面中,好多类似 Notice: Use of undefined constant titl...