php convert datetime to UTC
Try the getTimezone and setTimezone, see the example
(But this does use a Class)
UPDATE:
Without any classes you could try something like this:
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
NOTE: You might need to set the timezone back to the original as well
PHP Convert Local time to UTC DateTime
You will need to pass the date to strtotime
before you change the timezone with date_default_timezone_set
. Like so:
date_default_timezone_set('Asia/Calcutta');
$datetime = "2016-05-05 18:33:00";
$asia_timestamp = strtotime($datetime);
echo date_default_timezone_get()."<br>"; // Asia/Calcutta
date_default_timezone_set('UTC');
echo date_default_timezone_get()."<br>"; //UTC
echo $utcDateTime = date("Y-m-d H:i:s", $asia_timestamp);
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.
PHP: Convert Local Time to UTC
You can use DateTime
, example bellow:
<?php
$datetime = '08/22/2015 10:56 PM';
$tz_from = 'America/New_York';
$tz_to = 'UTC';
$format = 'Ymd\THis\Z';
$dt = new DateTime($datetime, new DateTimeZone($tz_from));
$dt->setTimeZone(new DateTimeZone($tz_to));
echo $dt->format($format) . "\n";
$minutes = 30;
$dt->add(new DateInterval('PT' . $minutes . 'M'));
echo $dt->format($format) . "\n";
How to convert DateTime in UTC format?
Pretty straightforward using DateTime objects
$string = "2015-03-17T07:46:52+0100";
$dt = new DateTime($string);
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s');
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
Parse a UTC date, output local datetime
You need to change the timezone of the date (using DateTime::setTimeZone()
), not the default timezone:
date_default_timezone_set('UTC');
$dt = new DateTime("2021-03-31 23:46:14");
$dt->setTimeZone(new DateTimeZone("Europe/paris")); // change date timezone
echo $dt->format('m/d/Y, H:i:s');
Output:
04/01/2021, 01:46:14
Changing the default timezone affects the new DateTime()
, not the format()
result.
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
Related Topics
Calling C/C++ Library Function from PHP
Retrieve All Hashtags from a Tweet in a PHP Function
Preg_Match(); - Unknown Modifier '+'
Array_Multisort and Dynamic Variable Options
You Must Enable the Openssl Extension to Download Files via Https
PHP Replacing Special Characters Like à->A, è->E
How to Send Cookies Using PHP Curl in Addition to Curlopt_Cookiefile
How to Modify Xml File Using PHP
How to Use Cookies Across Two Different Domains
Sqlstate[Hy000] [1045] Access Denied for User 'Username'@'Localhost' Using Cakephp
Convert Ascii to Utf-8 Encoding
How to Group by and Sum PHP Array
PHP Objects VS Arrays -- Performance Comparison While Iterating