Convert Utc Dates to Local Time in PHP

Convert UTC dates to local time in PHP

date() and localtime() both use the local timezone for the server unless overridden; you can override the timezone used with date_default_timezone_set().

http://www.php.net/manual/en/function.date-default-timezone-set.php

http://us3.php.net/manual/en/function.date.php

http://php.net/manual/en/function.localtime.php

PHP convert UTC time to local time

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

How to convert UTC timestamp Value to local date time

You need to change the timezone after to define the timestamp in GMT.

$timestamp = 1615958170523/1000;

$myDateTime = \DateTime::createFromFormat('U', (int)$timestamp);
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 05:16:10

$myDateTime->setTimezone(new \DateTimeZone('Europe/Paris'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-17 06:16:10

$myDateTime->setTimezone(new \DateTimeZone('America/Denver'));
echo $myDateTime->format('Y-m-d H:i:s'), PHP_EOL; // 2021-03-16 23:16:10

See DateTime::setTimezone() documentation

How to convert UTC datetime into PST datetime in php

Like this.

$utc_date = '2020-07-31T00:00:00.000Z';
$jsDateTS = strtotime($utc_date);
if ($jsDateTS !== false)
echo date('Y-m-d H:i:s', $jsDateTS );

Edit: Changed code to include timezone change.

$utc_date = '2020-07-31T00:00:00.000Z';
$timestamp = strtotime($utc_date);
$date = new DateTime();
$date->setTimestamp($timestamp);
$date->setTimezone(new \DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d H:i:s') . "\n";

Working Example.

Convert datetime UTC to local time in PHP

The date string already contains the Zulu timezone. So you need to create a new DateTime object, and then set the new timezone afterwards.

$date = new Datetime('2018-07-03T21:00:00.000Z');
$date->setTimezone(new DateTimeZone('-4.0'));

var_dump($date->format('Y-m-d H:i:s')); // 2018-07-03 17:00:00

PHP UTC to Local Time

This will do what you want using PHPs native DateTime and DateTimeZone classes:

$utc_date = DateTime::createFromFormat(
'Y-m-d G:i',
'2011-04-27 02:45',
new DateTimeZone('UTC')
);

$nyc_date = $utc_date;
$nyc_date->setTimeZone(new DateTimeZone('America/New_York'));

echo $nyc_date->format('Y-m-d g:i A'); // output: 2011-04-26 10:45 PM

See DateTime::createFromFormat man page for more information.

After some experimentation between time zones that do and do not currently have DST I have discovered that this will take DST into account. The same conversion using my method above renders the same resulting time.

converting timestamp to local time with PHP

By default DateTime::construct uses your local timezone. However your time is in UTC, so you need to tell the constructor that, and then modify the timezone:

$date = '2018-10-13 00:00:00';
$l10nDate = new DateTime($date, new DateTimeZone('UTC'));
$l10nDate->setTimeZone(new DateTimeZone('America/Vancouver'));
echo $l10nDate->format('Y-m-d H:i:s');

Output

2018-10-12 17:00:00

PHP converting UTC to Local Time

You can do this using native php without using Carbon:

$time = '2015-10-02 16:34:00+08';
$date = DateTime::createFromFormat('Y-m-d H:i:s+O', $time);
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Asia/Singapore'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;
$date->setTimeZone(new DateTimeZone('Etc/UTC'));
print $date->format('Y-m-d H:i:s') . PHP_EOL;

How to convert UTC datetime to another timezone?

Use DateTime and DateTimeZone.

$date = new DateTime('2012-07-16 01:00:00 +00');
$date->setTimezone(new DateTimeZone('Europe/Moscow')); // +04

echo $date->format('Y-m-d H:i:s'); // 2012-07-15 05:00:00


Related Topics



Leave a reply



Submit