456543646346
456543646346

Reputation: 993

converting a time to a different timezone with php

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

Upvotes: 8

Views: 28276

Answers (6)

Majeed A Alhassan
Majeed A Alhassan

Reputation: 31

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

?>

Upvotes: 3

ssaltman
ssaltman

Reputation: 3713

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 https://www.php.net/manual/en/timezones.php

Upvotes: 1

user729928
user729928

Reputation: 713

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

Upvotes: 24

jh314
jh314

Reputation: 27792

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');

Upvotes: 3

Ricky H
Ricky H

Reputation: 117

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

Upvotes: 6

Fabio
Fabio

Reputation: 23490

You can use date_default_timezone_set function to change loacal time zone

Example

date_default_timezone_set('Europe/Paris');

Upvotes: 1

Related Questions