Converting Between Timezones in PHP

Converting between timezones in PHP

I would not use date_default_timezone_set for general TZ conversions. (To clarify... if this is for display purposes, script wide, then using the default timezone is a reasonable thing to do.)

Instead, I would use something like:

$tz = new DateTimeZone('America/Los_Angeles');

$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimezone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";

DateTime input convert between timezones in PHP

Perhaps this will work

$test = new DateTime($_POST['fromDate'], new DateTimeZone('America/Los_Angeles'));
$test->setTimezone(new DateTimeZone('Europe/Paris'));
echo $test->format('Y-m-d\TH:i:s');

At least that is how it is done in php manual here: http://us2.php.net/manual/en/datetime.settimezone.php

How to convert between time zones in PHP using the DateTime class?

What you're looking for is this:

$triggerOn = '04/01/2013 03:08 PM';
$user_tz = 'America/Los_Angeles';

echo $triggerOn; // echoes 04/01/2013 03:08 PM

$schedule_date = new DateTime($triggerOn, new DateTimeZone($user_tz) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$triggerOn = $schedule_date->format('Y-m-d H:i:s');

echo $triggerOn; // echoes 2013-04-01 22:08:00

Timezone conversion in php

You can use the datetime object or their function aliases for this:

Example (abridged from PHP Manual)

date_default_timezone_set('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');

Edit regarding comments

but i cannt use this method because i need to show date in different time zones as the user login from different locations

That's not a problem. When a user logs in, you determine his timezone and set it to your DateTime object just like shown. I'm using a similar approach in one of my projects and it works like a charm.

in the database i need to get the dates in any single timezone, then only it can be processed properly

You store the time either as a timestamp or a datetime in one timezone. When you query a DateTime field, you either convert the time in a DateTime object to this timezone or - if your db supports it - query with the selected timezone.

converting a time to a different timezone with php

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

How to convert time from one timezone to another in PHP

date_default_timezone_set('Europe/London');

$datetime = new DateTime('2008-08-03 12:35:23');
echo $datetime->format('Y-m-d H:i:s') . "\n";
$la_time = new DateTimeZone('America/Los_Angeles');
$datetime->setTimezone($la_time);
echo $datetime->format('Y-m-d H:i:s');

Convert time and date from 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') . "\n";
?>

The above examples will output:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

found on DateTime Manual on php.net

EDIT:
Like Pekka said: The DateTime class exists from 5.2 on and there you first have to find out which of the methods are realy implemented and which one only exist from 5.3 on.

Using PHP DateTime to parse and convert timezone PST to PDT (GMT -8 to GMT -7)

Not the greatest but it's the only way I can think of to do this

$tz = new DateTimeZone('America/Los_Angeles');  
$saleEndDate = new DateTime("2016-11-07T17:30:00-08:00");
$saleEndDate->setTimezone($tz);
$stamp = $saleEndDate->format('U');
$zone = $tz->getTransitions($stamp, $stamp);
if(!$zone[0]['isdst']) $saleEndDate->modify('+1 hour');
echo $saleEndDate->format('Y-m-d H:i:s');

What I'm doing here is using the DateTimeZone::getTransitions function to determine if the date you provided is DST or not. If it isn't, we add one hour. Note that this does not change the time zone, it just corrects for the DST shift

You can see it in action here

Converting Time with timezone PHP

You can try this code, this will work for you and you can set the timezone as per your need.

// Input  : '2021-01-20T19:03:52.355+0300';
// Output : '20-01-2021 23.03.52,355000 +07:00';

date_default_timezone_set('Europe/London');
$datetime = new DateTime('2021-01-20T19:03:52.355+0300');

// timezone to convert.
$la_time = new DateTimeZone('Asia/Krasnoyarsk');
$datetime->setTimezone($la_time);
echo $datetime->format('d-m-Y H.i.s,u P');

Output:

20-01-2021 23.03.52,355000 +07:00


Related Topics



Leave a reply



Submit