更改服务器时间的PHP脚本

更改服务器时间的PHP脚本,php,server,Php,Server,我在一些客户端网站上使用HostGator,它是一个共享云服务器设置。我需要运行以下脚本: <?php date_default_timezone_set("America/Los_Angeles"); ?> 因为服务器时间设置为不靠近美国的时区。。如何执行该脚本以更改服务器时间。。他们不支持(HostGator)更改服务器时间,但建议运行上述脚本 如果您有任何帮助,我们将不胜感激。服务器,假设它们运行在世界上不同的地理位置,根据时区的不同,都将具有不同的日

我在一些客户端网站上使用HostGator,它是一个共享云服务器设置。我需要运行以下脚本:

<?php
date_default_timezone_set("America/Los_Angeles");
?>

因为服务器时间设置为不靠近美国的时区。。如何执行该脚本以更改服务器时间。。他们不支持(HostGator)更改服务器时间,但建议运行上述脚本


如果您有任何帮助,我们将不胜感激。

服务器,假设它们运行在世界上不同的地理位置,根据时区的不同,都将具有不同的日期时间。迁移到位于不同时区的服务器时会发生什么情况?您的所有日期时间都将发生变化,这可能会导致一些相当严重的问题

您还可能遇到这样的场景:应用程序服务器(比如PHP)在一个时区运行,而数据库服务器(比如PostgreSQL)在一个单独的时区运行——那么您该怎么办,其中一些日期时间可能由PHP生成,而另一些则由PostgreSQL生成

答案是考虑时区

PHP和Python等编程语言以及MySQL和PostgreSQL等数据库不仅能够在严格的服务器特定时间工作,还能够在更灵活的时区工作

在诸如PostgreSQL之类的数据库中,可以将数据类型设置为带时区的时间戳

在PHP中,可以使用时区修复服务器默认显示的日期时间与用户期望的日期时间之间的差异

下面是一个PHP示例,它使您能够:

  • 获取服务器的当前日期时间
  • 将服务器的日期时间转换为用户所在的时区
  • 向用户显示转换后的日期时间
嗨,Jason,您需要在正在执行的脚本上运行,也就是说,它需要以代码的形式存在于您的“…php”文件中。
<?php
// Set the user's timezone:
$user_timezone = 'America/Detroit';

// Get the current datetime
$datetime = new DateTime();

// Display the server's datetime in human-readable and UNIX timestamp formats
print(
    "\nServer's Datetime: " . $datetime->format('Y-m-d H:i:s') . "" .
    "\nServer's Timestamp: " . $datetime->getTimestamp() . "" .
);

// Instantiate a DateTimeZone object representing the user's timezone
$timezone = new DateTimeZone( $user_timezone );

// Set the timezone on that datetime to be the user's timezone
$datetime->setTimezone( $timezone );

// Display the converted datetime to the user in human-readable format:
print(
    "\nUser's Datetime: " . $datetime->format('Y-m-d H:i:s') . "" .
    "\nUsers's Timestamp: " . $datetime->getTimestamp() . "" .
);
?>
<?php
print_r(
    timezone_identifiers_list()
);
>?>