converting a time to a different timezone with php

2019-01-20 08:57发布

$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 ?

6条回答
Melony?
2楼-- · 2019-01-20 09:38

You can use date_default_timezone_set function to change loacal time zone

Example

date_default_timezone_set('Europe/Paris');
查看更多
祖国的老花朵
3楼-- · 2019-01-20 09:40

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

$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

查看更多
SAY GOODBYE
4楼-- · 2019-01-20 09:47

Use the date_default_timezone_set() function of PHP.

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

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

查看更多
三岁会撩人
5楼-- · 2019-01-20 09:48

Check out 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');
查看更多
孤傲高冷的网名
6楼-- · 2019-01-20 09:55

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.

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

查看更多
在下西门庆
7楼-- · 2019-01-20 09:58
<?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;

?>
查看更多
登录 后发表回答