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

yipeiwu_com6年前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 巧用数组降低程序的时间复杂度

关于作者 王丹丹 , IBM 中国系统与技术中心软件工程师,自从 2006 年加入 IBM,一直从事 Web 系统设计和开发工作,有五年 PHP 应用程序设计开发经验。 通常开发人员在写...

PHP静态新闻列表自动生成代码

function CreateShtml()  {  ob_start(array("callback_CreateShtml","callback_GoT...

解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法

最近用ThinkPHP开发一个项目,集成了批量上传文件插件Uploadify,在谷歌Chrome和IE下都能正常上传,只有火狐下提示这个错误,网上找了很多解决办法,基本都说flash在f...

PHP中unset,array_splice删除数组中元素的区别

如果要在某个数组中删除一个元素,可以直接用的unset,但是数组的索引不会重排: <?php $arr = array('a','b','c','d'); unset...

php 开发中加密的几种方法总结

php 开发中加密的几种方法总结

1,使用crypt()函数进行加密 crypt()函数可以进行单项加密,具体语法如下: string crypt(string str[,tring salt]) 其中...