源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-08-28 22:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP获取中国时间(上海时区时间)及美国时间的方法
本文实例讲述了PHP获取中国时间(上海时区时间)及美国时间的方法。分享给大家供大家参考,具体如下: [b]中国时间:[/b]
/**
 * 获取中国时间,即上海时区时间
 * @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

[b]美国时区:[/b] 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

[b]PS:这里再为大家提供2款时间相关工具,供大家参考使用:[/b] [b]Unix时间戳(timestamp)转换工具: [/b][url=http://tools.jb51.net/code/unixtime]http://tools.jb51.net/code/unixtime[/url] [b]在线世界各地时间查询: [/b][url=http://tools.jb51.net/zhuanhuanqi/worldtime]http://tools.jb51.net/zhuanhuanqi/worldtime[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/96.htm]php日期与时间用法总结[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部