Setting timezone to UTC (0) in PHP(在 PHP 中将时区设置为 UTC (0))
问题描述
为什么会这样?
date_default_timezone_set('Australia/Currie');
但这似乎根本没有任何效果?
But this doesn't seem to take any effect at all?
date_default_timezone_set('UTC');
将时区设置为 UTC 时,此值不会改变:
This value doesn't change when setting the timezone to UTC:
echo date('Y-m-d H:i:s', time());
我使用的是php 5.2.13,我的服务器时区是:
I'm using php 5.2.13, and the timezone of my server is:
$server_tz = date_default_timezone_get();
echo $server_tz; //outputs 'America/Guayaquil'
这是原始代码:
echo time() . "<br>
";
date_default_timezone_set('UTC');
echo time() . "<br>
";
输出:
1317235130
1317235130
推荐答案
问题是您正在显示 time()
,它是基于 GMT/UTC 的 UNIX 时间戳.这就是它不改变的原因.另一方面,date()
格式化基于该时间戳的时间.
The problem is that you're displaying time()
, which is a UNIX timestamp based on GMT/UTC. That’s why it doesn’t change. date()
on the other hand, formats the time based on that timestamp.
timestamp 是自 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的秒数.
A timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
echo date('Y-m-d H:i:s T', time()) . "<br>
";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "<br>
";
这篇关于在 PHP 中将时区设置为 UTC (0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中将时区设置为 UTC (0)


基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01