在 php 中获取服务器时间 - 时区问题

2024-08-14php开发问题
0

本文介绍了在 php 中获取服务器时间 - 时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 shell 上,我的服务器时间是(简单的 date in bash):2010 年 8 月 29 日星期日 10:37:12 EDT

on shell, my server time is (simple date in bash): Sun Aug 29 10:37:12 EDT 2010

当我运行代码 php -r "echo date('Y-m-d H:i:s');" 我得到:2010-08-29 10:37:10

when I run the code php -r "echo date('Y-m-d H:i:s');" I get: 2010-08-29 10:37:10

但是当我执行一个具有 echo date('Ymd H:i:s'); 的 php 脚本时,我得到一个不同的时间 - 因为 php 认为时区是 UTC(服务器时间是 EDT).

but when I execute a php script that has echo date('Y-m-d H:i:s'); I get a different time- since php thinks the timezone is UTC (server time is EDT).

我的 php.ini 文件没有设置时区,我不想更改任何内容 - 因为我不知道它会对在此服务器上运行的其他站点产生什么影响.

My php.ini file has no timezone set, and I wouldnt like to change anything- as I have no idea what affect it would have on other sites that run on this server.

有没有一种简单的方法可以在为服务器设置的时区获取服务器时间?

Is there a simple way to get the server time- at the timezone that is set for the server?

推荐答案

根据php.ini 指令 手册页,如果您使用的是 5.3 之前的版本,则默认情况下不设置时区.然后,如果您查看 的页面date_default_timezone_get,它使用以下顺序获取时区:

According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)

如果所有这些都失败,则 UTC 是默认值,所以这可能是一个很好的起点.

UTC is the default if all of those fail, so that's probably a good starting point.

这篇关于在 php 中获取服务器时间 - 时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17