Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 847
Jon87 Avatar asked Jun 13 '13 23:06

Jon87


People also ask

How do I convert one time zone to another in PHP?

php $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; $date->setTimezone(new DateTimeZone('Pacific/Chatham')); echo $date->format('Y-m-d H:i:sP') .

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

How do I convert DateTime to timezone?

To do this, we will use the FindSystemTimeZoneById() of TimeZoneInfo class. This function takes Id as a parameter to return the equivalent time in timezone passed. In the example below, we are taking the current system time and converting it into “Central Standard Time”. DateTime currentTime = TimeZoneInfo.

What is UTC timezone PHP?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. To change the PHP timezone for an app, create a .user.ini file in the app's public directory with the following contents: date.timezone = America/Los_Angeles.


2 Answers

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

like image 158
user729928 Avatar answered Oct 17 '22 09:10

user729928


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

like image 6
Ricky H Avatar answered Oct 17 '22 08:10

Ricky H