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单例模式实现方法分析

本文实例讲述了php单例模式实现方法。分享给大家供大家参考。具体如下: <?php /** * @copyright 2013 maguowei.com * @au...

浅析php变量修饰符static的使用

静态变量仅在局部函数域中存在,但当程序执行离开此作用域时,其值并不丢失。看看下面的例子:复制代码 代码如下:function test(){static $a=0;$a++;echo $...

PHP中使用asort进行中文排序失效的问题处理

PHP中有非常方便的对数组进行重新排序的方法——asort,关于asort的使用方法可以看 这里 。但是asort对含有中文key的数组进行排序时,有时候并不是按照字母顺序。这主要是编码...

PHP类的声明与实例化及构造方法与析构方法详解

本文实例讲述了PHP类的声明与实例化及构造方法与析构方法。分享给大家供大家参考,具体如下: <?php class human{ public static $le...

PHP伪静态页面函数附使用方法

function MakeUrl($arr){           &nbs...