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)
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
