13

Am getting a UTC time from my server like the following format, my requirement is to convert the UTC time to local time. So users can see a user friendly time on their browser based on their timezone. Please help me to solve this issue. Thank you

$utc = "2014-05-29T04:54:30.934Z"

I have tried some methods but not working in my case

First

$time = strtotime($utc);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;

Second

$time = strtotime($utc .' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;
Dibish
  • 9,133
  • 22
  • 64
  • 106

10 Answers10

22

Simply use a DateTimeZone, eg

$dt = new DateTime($utc);
$tz = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after

$dt->setTimezone($tz);
echo $dt->format('Y-m-d H:i:s');

Demo ~ http://ideone.com/fkM4ct

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Thanks for the quick reply. How do i know the 'Asia/Kolkata' timezone for the user. The user may be any where for the world. Is it a static value? – Dibish May 29 '14 at 05:45
  • @Dibish I assumed you knew the required timezone. See [Martin's answer](http://stackoverflow.com/a/23926398/283366) regarding getting information from the client – Phil May 29 '14 at 05:47
16

The reason your code isn't working is most likely because your server is in UTC time. So the local time of the server is UTC.

Solution #1

One potential solution is to do the following server side and pass the epoch integer to the browser:

$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc); //returns an integer epoch time: 1401339270

Then use JavaScript to convert the epoch integer to the user's local time (the browser knows the user's timezone).

Solution #2

You can get the browser to send you the user's timezone. Then you can use this information to calculate the date string server side instead of browser side. See more here: https://stackoverflow.com/a/5607444/276949

This will give you the user's offset (-7 hours). You can use this information to set the timezone by looking here: Convert UTC offset to timezone or date

Community
  • 1
  • 1
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • 1
    Solution #2 is ill-advised, as you can only get the *current* time zone offset directly from JavaScript. You can *guess* at the time zone with [jsTimeZoneDetect](http://pellepim.bitbucket.org/jstz/), but that will only get you so far. Solution #1 is better, but [has it's own set of problems](http://codeofmatt.com/2013/06/07/javascript-date-type-is-horribly-broken/). – Matt Johnson-Pint Aug 21 '14 at 18:02
  • @MattJohnson I guess you could extract the timezone from `new Date()` which returns `Tue Oct 25 2016 12:43:28 GMT-0400 (EDT)`, but EDT/EST isn't very unique if I recall (there are various timezones in the world which might overlap with the 3 character timezone representation). Perhaps a mix of the offset and the 3 character representation might work. – Martin Konecny Oct 25 '16 at 16:44
  • Unfortunately, the value in parenthesis is implementation specific, and varies considerably across platforms. It's not always an abbreviation, and yes - abbreviations are often ambiguous (there are 5 different CST's, for example). – Matt Johnson-Pint Oct 26 '16 at 16:20
  • YOU NEED TO USE `;` – thedanotto Feb 13 '17 at 19:56
  • One other deficiency (for certain process or server related needs) is that this drops the milliseconds. For those who need such accuracy, that can be resolved by tailing the milliseconds (of the source) onto the result and creating a numeric instead of an int. – Robert Mauro Feb 01 '19 at 16:42
4

If this is not done via a user profile setting, it is probably easier and simpler to use a javascript time library eg. moment.js (http://momentjs.com/) to solve this problem (send all date/times in UTC and then convert them on the client end).

Javascript eg. (I am sure this can be achieved without creating two moment objects (fix that at your leisure).

function utcToLocalTime(utcTimeString){
    var theTime  = moment.utc(utcTimeString).toDate(); // moment date object in local time
    var localTime = moment(theTime).format('YYYY-MM-DD HH:mm'); //format the moment time object to string

    return localTime;
}

php example for server side resolution (only use one of these solutions, both together will produce an incorrect time on the client end)

/**
 * @param $dateTimeUTC
 * @param string $timeZone
 * @param string $dateFormat
 * @return string
 *
 * epoch to datetime (utc/gmt)::
 * gmdate('Y-m-d H:i:s', $epoch);
 */
function dateToTimezone($timeZone = 'UTC', $dateTimeUTC = null, $dateFormat = 'Y-m-d H:i:s'){

    $dateTimeUTC = $dateTimeUTC ? $dateTimeUTC : date("Y-m-d H:i:s");

    $date = new DateTime($dateTimeUTC, new DateTimeZone('UTC'));
    $date->setTimeZone(new DateTimeZone($timeZone));

    return $date->format($dateFormat);
}
Robin
  • 1,395
  • 1
  • 9
  • 9
2

Use function date_default_timezone_set before there you want to local time after that again setup UTC Europe/Lisbon timezone List of Supported Timezones

  <?php

$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc);
echo "<br/>Defualt UTC/server time".$dateInLocal = date("Y-m-d H:i:s", $time);

//your local time zone put here
date_default_timezone_set('Asia/Kolkata');

echo "<br/>Local time". $dateInLocal = date("Y-m-d H:i:s", $time);
?>
Girish
  • 11,907
  • 3
  • 34
  • 51
2

Just send the UTC time to the browser. Use JavaScript to parse it with a Date object, or with moment.js, then output it as text.

By doing it in client-side JavaScript, you don't need to be aware of the user's time zone. The browser will be responsible for that instead.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
2
#cnvt_usrTime_to_UTC 0

function cnvt_usrTime_to_UTC($dt_start_time_formate,$UTC_TimeZone){

  $LocalTime_start_time = new DateTime( $dt_start_time_formate );
  $tz_start = new DateTimeZone( $UTC_TimeZone );
  $LocalTime_start_time->setTimezone( $tz_start );
  $array_start_time = (array) $LocalTime_start_time;

  return $UTC_Time_Start_Time = $array_start_time['date'];
}

call the function cnvt_usrTime_to_UTC($dt_start_time_formate,'UTC');

#cnvt_UTC_to_usrTime
function cnvt_UTC_to_usrTime($Date_Time,$User_time_Zone){

    date_default_timezone_set('UTC');

    $LocalTime_start_time = new DateTime( $Date_Time );
    $tz_start = new DateTimeZone( $User_time_Zone );
    $LocalTime_start_time->setTimezone( $tz_start );
    $start_date_time = (array) $LocalTime_start_time;

    return $StartDateTime = $start_date_time['date'];
}

call the function

cnvt_UTC_to_usrTime($value_edit['dt_start_time'],Asia/Kolkata);
Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Divyesh
  • 329
  • 3
  • 17
1

try with date_default_timezone_set() and set date according timezone

date_default_timezone_set('asia/kolkata');
$utc = "2014-05-29T04:54:30.934Z";
$time = strtotime($utc);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;  //2014-05-29 10:24:30
echo '<br>';
date_default_timezone_set('America/Chicago'); 
$time = strtotime($utc .' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal; //2014-05-28 23:54:30 
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44
1

$savedtime = '2019-05-25 00:01:13'; //this is America/Chicago time
$servertime = ini_get('date.timezone');
$time = strtotime($savedtime . $servertime);
$dateInLocal = date("Y-m-d H:i:s", $time);
echo $dateInLocal;

display value according to local timezone
2019-05-25 10:31:13  // this is Asia/Kolkata time
Ricky Riccs
  • 41
  • 1
  • 5
0
$utc_date = '2021-10-07T11:01:07';
//local timezone or ant timezone you want
$timezone = 'America/Los_Angeles';
$timestamp = strtotime($utc_date);
$date = new DateTime();
$date->setTimestamp($timestamp);

$date->setTimezone(new \DateTimeZone($timezone));
echo $date->format('m-d-Y g:i A') . "\n";
ravina vala
  • 119
  • 4
-1
protected function get_timemillizone_date($timezone_id)
{
    date_default_timezone_set($timezone_id);
    $availdate = date('Y-m-d');
    $stamp = strtotime($availdate); // get current date timestamp
    $currenttimemill = $stamp * 1000;
    return $currenttimemill;
}

$timezone_id = "Asia/Kolkata";
$DATA = $this->get_timemillizone_date($timezone_id);
echo $DATA;
  • Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem. – Sven Eberth Jun 04 '21 at 15:00