Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert time and date from one time zone to another in PHP

Tags:

timezone

php

Basically what I need is an script that, when provided with a time and a timezone can return the time in another time zone.

My main issues are:

  • Where to get the time offset from GMT from - is there a public database available for this?
  • How to also take into consideration the daylight saving time (DST) differences as well.
  • How to nicely wrap it all up inside an PHP class - or is there such a class already available?
like image 855
titel Avatar asked Oct 11 '10 10:10

titel


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.

What is UTC time zone in 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. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.

How do I switch from one timezone to another in SQL Server?

SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.


3 Answers

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

like image 87
ITroubs Avatar answered Sep 30 '22 09:09

ITroubs


try this, it might help :)

function converToTz($time="",$toTz='',$fromTz='')
    {   
        // timezone by php friendly values
        $date = new DateTime($time, new DateTimeZone($fromTz));
        $date->setTimezone(new DateTimeZone($toTz));
        $time= $date->format('Y-m-d H:i:s');
        return $time;
    }

A bit description: The function takes 3 inputs, time to convert, timezone to convert to, current timezone and returns the output in the specified format.

like image 32
Shubham Mathur Avatar answered Sep 30 '22 10:09

Shubham Mathur


I know its late. For anyone who would want simple function to convert utc to any local time zone

function UTCTimeToLocalTime($time, $tz = '', $FromDateFormat = 'Y-m-d H:i:s', $ToDateFormat = 'Y-m-d H:i:s')
{
if ($tz == '')
    $tz = date_default_timezone_get();

$utc_datetime = DateTime::createFromFormat($FromDateFormat, $time, new
    DateTimeZone('UTC'));
$local_datetime = $utc_datetime;

$local_datetime->setTimeZone(new DateTimeZone($tz));
return $local_datetime->format($ToDateFormat);
}

 echo UTCTimeToLocalTime('2015-07-01 13:30:00','America/Denver');
like image 33
srp Avatar answered Sep 30 '22 11:09

srp