PHP converts UTC time to local time

I get UTC time from my server, as in the following format, my requirement is to convert UTC time to local time. In this way, users can see user-friendly time in their browser based on their time zone. Please help me solve this problem. Thanks you

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

I tried some methods but did not work in my case

First

 $time = strtotime($utc); $dateInLocal = date("Ymd H:i:s", $time); echo $dateInLocal; 

Second

 $time = strtotime($utc .' UTC'); $dateInLocal = date("Ymd H:i:s", $time); echo $dateInLocal; 
+9
timezone php datetime
source share
8 answers

The reason your code doesn't work is most likely because your server is in UTC. Therefore, the local server time is UTC.

Decision No. 1

One of the possible solutions is to execute the following server part and pass the integer of the era to the browser:

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

Then use JavaScript to convert an integer number of eras to the user's local time (the browser knows the user's time zone).

Decision number 2

You can force the browser to send you the user's time zone. You can then use this information to calculate the server line of the date string, not the browser side. See more details here: https://stackoverflow.com/a/318677/

This will give you user bias (-7 hours). You can use this information to set the time zone by looking here: Convert UTC Offset to Time Zone or Date

+12
source share

Just use DateTimeZone for example

 $dt = new DateTime($utc); $tz = new DateTimeZone('Asia/Kolkata'); // or whatever zone you're after $dt->setTimezone($tz); echo $dt->format('Ymd H:i:s'); 

Demo ~ http://ideone.com/fkM4ct

+13
source share

If this is not done by setting a user profile, it may be easier and easier to use a javascript time library, for example. moment.js ( http://momentjs.com/ ) to solve this problem (send all dates / times to UTC and then convert them to the end of the client).

Javascript for example. (I am sure that this can be achieved without creating two objects of the moment (fix this 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; } 

An example php for server-side resolution (use only one of these solutions, both together will create the wrong time on the client side)

 /** * @param $dateTimeUTC * @param string $timeZone * @param string $dateFormat * @return string * * epoch to datetime (utc/gmt):: * gmdate('Ymd H:i:s', $epoch); */ function dateToTimezone($timeZone = 'UTC', $dateTimeUTC = null, $dateFormat = 'Ymd H:i:s'){ $dateTimeUTC = $dateTimeUTC ? $dateTimeUTC : date("Ymd H:i:s"); $date = new DateTime($dateTimeUTC, new DateTimeZone('UTC')); $date->setTimeZone(new DateTimeZone($timeZone)); return $date->format($dateFormat); } 
+3
source share

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

When doing this in client-side JavaScript, you do not need to know the user's time zone. Instead, the browser will be responsible for this.

+2
source share
 #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']; } 

function call 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 function

 cnvt_UTC_to_usrTime($value_edit['dt_start_time'],Asia/Kolkata); 
+2
source share

Use the date_default_timezone_set function before you want to use local time after that, again set the UTC Europe/Lisbon List of Supported Timezones time zone

  <?php $utc = "2014-05-29T04:54:30.934Z"; $time = strtotime($utc); echo "<br/>Defualt UTC/server time".$dateInLocal = date("Ymd H:i:s", $time); //your local time zone put here date_default_timezone_set('Asia/Kolkata'); echo "<br/>Local time". $dateInLocal = date("Ymd H:i:s", $time); ?> 
+1
source share

try date_default_timezone_set () and set the date according to the time zone

 date_default_timezone_set('asia/kolkata'); $utc = "2014-05-29T04:54:30.934Z"; $time = strtotime($utc); $dateInLocal = date("Ymd 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("Ymd H:i:s", $time); echo $dateInLocal; //2014-05-28 23:54:30 
0
source share

 $savedtime = '2019-05-25 00:01:13'; //this is America/Chicago time $servertime = ini_get('date.timezone'); $time = strtotime($savedtime . $servertime); $dateInLocal = date("Ymd H:i:s", $time); echo $dateInLocal; display value according to local timezone 2019-05-25 10:31:13 // this is Asia/Kolkata time 
0
source share

All Articles