php 使用php将时间转换为不同的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17098751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:15:46  来源:igfitidea点击:

converting a time to a different timezone with php

phpdatetimetimezone

提问by Jon87

$timeposted = "7:10pm";

$timeposted = "7:10pm";

This value is currently Canada time (quebec). I'm trying to find a way to convert it to France's time. How can i do that ?

该值当前是加拿大时间(魁北克)。我正在尝试找到一种方法将其转换为法国时间。我怎样才能做到这一点 ?

采纳答案by Ricky H

Use the date_default_timezone_set()function of PHP.

使用PHP的date_default_timezone_set()函数。

If you want to change it to France you would use the

如果你想把它改成法国,你可以使用

date_default_timezone_set('Europe/Paris');

a list of Supported Timezones can be found here: http://www.php.net/manual/en/timezones.php

可以在此处找到支持的时区列表:http: //www.php.net/manual/en/timezones.php

The functionality of date_default_timezone_set() can be found here: http://php.net/manual/en/function.date-default-timezone-set.php

date_default_timezone_set() 的功能可以在这里找到:http://php.net/manual/en/function.date-default-timezone-set.php

回答by user729928

Assuming that your PHP configuration is set to the Quebec time, you can convert it to France's timezone by doing the following:

假设您的 PHP 配置设置为魁北克时间,您可以通过执行以下操作将其转换为法国的时区:

$date = new DateTime('7:10pm', new DateTimeZone('Europe/Paris'));
echo $date->format('Y-m-d H:i:sP');

Or, if your server is not set to the Quebec timezone you can:

或者,如果您的服务器未设置为魁北克时区,您可以:

$date = new DateTime('7:10pm', new DateTimeZone('America/Montreal'));

$date->setTimezone(new DateTimeZone('Europe/Paris'));

echo $date->format('Y-m-d H:i:sP');

which returns

返回

2013-06-14 01:10:00+02:00 

You can read more about PHP and timezones here: http://www.php.net/manual/en/datetime.settimezone.php

您可以在此处阅读有关 PHP 和时区的更多信息:http: //www.php.net/manual/en/datetime.settimezone.php

回答by jh314

Check out DateTime::setTimezone:

查看DateTime::setTimezone

Example

例子

date_default_timezone_set('America/Los_Angeles');

$datetime = new DateTime('2013-06-13 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$timeEurope = new DateTimeZone('Europe/London');
$datetime->setTimezone($timeEurope);
echo $datetime->format('Y-m-d H:i:s');

回答by Majeed A Alhassan

<?php
date_default_timezone_set('America/Los_Angeles');//Your global default timeZone.

function convertTimeZone($oTime, $oTimeZone, $nTimeZone) 
{
//Parameter string $oTime is original time to be converted from in format F-d-Y h:i:s
//Parameter string $oTimeZone is timezone to be conveted from- Timezone of $oTimeZone
//Parameter string $nTimeZone is timezone to be conveted to

date_default_timezone_set($oTimeZone);  //Change default timezone to old timezone within this function only.

$originalTime = new DateTime($oTime);

$originalTime->setTimeZone(new DateTimeZone($nTimeZone)); //Convert to desired TimeZone.

date_default_timezone_set('America/Los_Angeles') ; //Reset default TimeZone according to your global settings.

return $originalTime->format('F-d-Y h:i:s A'); //Return converted TimeZone.
} 

$LATime = convertTimeZone("2011-01-07 19:55:00","America/Chicago", "America/Los_Angeles");

echo $LATime;

?>

回答by ssaltman

This is my function that take time from a mysql db (which I've stored entirely in UTC) and converts to a new timezone and formats it simply.

这是我的函数,它从 mysql db(我完全存储在 UTC 中)中花费时间并转换为新的时区并简单地对其进行格式化。

function changetimefromUTC($time, $timezone) {
    $changetime = new DateTime($time, new DateTimeZone('UTC'));
    $changetime->setTimezone(new DateTimeZone($timezone));
    return $changetime->format('m/d/y h:i a');
}

This is a list of supported timezones http://us1.php.net/manual/en/timezones.php

这是支持的时区列表 http://us1.php.net/manual/en/timezones.php

回答by Fabio

You can use date_default_timezone_setfunction to change loacal time zone

您可以使用date_default_timezone_set函数更改本地时区

Example

例子

date_default_timezone_set('Europe/Paris');