I am in need of an easy way to convert a date time stamp to UTC [from whatever timezone the server is in] HOPEFULLY without using any libraries.
asked Jan 19, 2010 at 17:55
0
Use strtotime to generate a timestamp from the given string [interpreted as local time] and use gmdate to get it as a formatted UTC date back.
Example
As requested, here’s a simple example:
echo gmdate['d.m.Y H:i', strtotime['2012-06-28 23:55']];
answered Jan 19, 2010 at 18:06
pokepoke
347k66 gold badges534 silver badges580 bronze badges
12
Using DateTime:
$given = new DateTime["2014-12-12 14:18:00"];
echo $given->format["Y-m-d H:i:s e"] . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone[new DateTimeZone["UTC"]];
echo $given->format["Y-m-d H:i:s e"] . "\n"; // 2014-12-12 07:18:00 UTC
answered Dec 12, 2014 at 7:24
joerxjoerx
1,9681 gold badge15 silver badges18 bronze badges
2
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[] . "
"];
echo[date["Y-d-mTG:i:sz",$the_date] . "
"];
echo[date_default_timezone_set["UTC"] . "
"];
echo[date["Y-d-mTG:i:sz", $the_date] . "
"];
NOTE: You might need to set the timezone back to the original as well
answered Jan 19, 2010 at 18:08
Phill PaffordPhill Pafford
81.7k90 gold badges260 silver badges380 bronze badges
7
Do this way:
gmdate['Y-m-d H:i:s', $timestamp]
or simply
gmdate['Y-m-d H:i:s']
to get "NOW" in UTC.
Check the reference:
//www.php.net/manual/en/function.gmdate.php
laurent
84.8k73 gold badges270 silver badges411 bronze badges
answered Jul 3, 2011 at 5:32
AttilioAttilio
2,0812 gold badges21 silver badges20 bronze badges
0
If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.
date_default_timezone_set['Europe/Stockholm'];
print date['Y-m-d H:i:s',strtotime["2009-01-01 12:00"." UTC"]]."\n";
print date['Y-m-d H:i:s',strtotime["2009-06-01 12:00"." UTC"]]."\n";
This will print this:
2009-01-01 13:00:00
2009-06-01 14:00:00
And as you can see it takes care of the daylight savings time problem as well.
A little strange way to solve it.... :]
answered Feb 14, 2010 at 21:50
JohanJohan
19.4k28 gold badges91 silver badges110 bronze badges
3
Convert local time zone string to UTC string.
e.g. New Zealand Time Zone
$datetime = "2016-02-01 00:00:01";
$given = new DateTime[$datetime, new DateTimeZone["Pacific/Auckland"]];
$given->setTimezone[new DateTimeZone["UTC"]];
$output = $given->format["Y-m-d H:i:s"];
echo [$output];
- NZDT: UTC+13:00
if $datetime = "2016-02-01 00:00:01", $output = "2016-01-31 11:00:01";
if $datetime = "2016-02-29 23:59:59", $output = "2016-02-29 10:59:59"; - NZST: UTC+12:00
if $datetime = "2016-05-01 00:00:01", $output = "2016-04-30 12:00:01";
if $datetime = "2016-05-31 23:59:59", $output = "2016-05-31 11:59:59";
//en.wikipedia.org/wiki/Time_in_New_Zealand
answered May 4, 2016 at 5:32
Frank HouFrank Hou
1,6181 gold badge15 silver badges11 bronze badges
0
If you don't mind using PHP's DateTime class, which has been available since PHP 5.2.0, then there are several scenarios that might fit your situation:
If you have a
$givenDt
DateTime object that you want to convert to UTC then this will convert it to UTC:$givenDt->setTimezone[new DateTimeZone['UTC']];
If you need the original
$givenDt
later, you might alternatively want to clone the given DateTime object before conversion of the cloned object:$utcDt = clone $givenDt; $utcDt->setTimezone[new DateTimeZone['UTC']];
If you only have a datetime string, e.g.
$givenStr = '2018-12-17 10:47:12'
, then you first create a datetime object, and then convert it. Note this assumes that$givenStr
is in PHP's configured timezone.$utcDt = [new DateTime[$givenStr]]->setTimezone[new DateTimeZone['UTC']];
If the given datetime string is in some timezone different from the one in your PHP configuration, then create the datetime object by supplying the correct timezone [see the list of timezones PHP supports]. In this example we assume the local timezone in Amsterdam:
$givenDt = new DateTime[$givenStr, new DateTimeZone['Europe/Amsterdam']]; $givenDt->setTimezone[new DateTimeZone['UTC']];
answered Dec 17, 2018 at 10:00
As strtotime requires specific input format, DateTime::createFromFormat could be used [php 5.3+ is required]
// set timezone to user timezone
date_default_timezone_set[$str_user_timezone];
// create date object using any given format
$date = DateTime::createFromFormat[$str_user_dateformat, $str_user_datetime];
// convert given datetime to safe format for strtotime
$str_user_datetime = $date->format['Y-m-d H:i:s'];
// convert to UTC
$str_UTC_datetime = gmdate[$str_server_dateformat, strtotime[$str_user_datetime]];
// return timezone to server default
date_default_timezone_set[$str_server_timezone];
answered Aug 18, 2012 at 19:17
I sometime use this method:
// It is not importnat what timezone your system is set to.
// Get the UTC offset in seconds:
$offset = date["Z"];
// Then subtract if from your original timestamp:
$utc_time = date["Y-m-d H:i:s", strtotime[$original_time." -".$offset." Seconds"]];
Works all MOST of the time.
answered Apr 16, 2012 at 14:46
aorcsikaorcsik
14.8k4 gold badges38 silver badges49 bronze badges
1
answered Jan 19, 2010 at 18:06
psychotikpsychotik
37k34 gold badges99 silver badges135 bronze badges
With PHP 5 or superior, you may use datetime::format function [see documentation //us.php.net/manual/en/datetime.format.php]
echo strftime[ '%e %B %Y' ,
date_create_from_format['Y-d-m G:i:s', '2012-04-05 11:55:21']->format['U']
]; // 4 May 2012
answered Aug 9, 2012 at 11:05
MUY BelgiumMUY Belgium
2,2124 gold badges30 silver badges42 bronze badges
try
echo date['F d Y', strtotime['2010-01-19 00:00:00']];
will output:
January 19 2010
you should change format time to see other output
answered Oct 18, 2011 at 16:42
General purpose normalisation function to format any timestamp from any timezone to other. Very useful for storing datetimestamps of users from different timezones in a relational database. For database comparisons store timestamp as UTC and use with gmdate['Y-m-d H:i:s']
/**
* Convert Datetime from any given olsonzone to other.
* @return datetime in user specified format
*/
function datetimeconv[$datetime, $from, $to]
{
try {
if [$from['localeFormat'] != 'Y-m-d H:i:s'] {
$datetime = DateTime::createFromFormat[$from['localeFormat'], $datetime]->format['Y-m-d H:i:s'];
}
$datetime = new DateTime[$datetime, new DateTimeZone[$from['olsonZone']]];
$datetime->setTimeZone[new DateTimeZone[$to['olsonZone']]];
return $datetime->format[$to['localeFormat']];
} catch [\Exception $e] {
return null;
}
}
Usage:
$from = ['localeFormat' => "d/m/Y H:i A", 'olsonZone' => 'Asia/Calcutta']; $to = ['localeFormat' => "Y-m-d H:i:s", 'olsonZone' => 'UTC']; datetimeconv["14/05/1986 10:45 PM", $from, $to]; // returns "1986-05-14 17:15:00"
answered Dec 9, 2016 at 5:26
SandeepSandeep
27.1k3 gold badges30 silver badges23 bronze badges
As an improvement on Phill Pafford's answer [I did not understand his 'Y-d-mTG:i:sz' and he suggested to revert timezone]. So I propose this [I complicated by changing the HMTL format in plain/text...]:
answered Sep 11, 2017 at 17:31
2
alternatively you can try this:
this will output :
2017-10-25 17:13:20 Asia/Singapore
you can use this inside the value attribute of a text input box if you only want to display a read-only date.
remove the 'e' if you do not wish to show your region/country.
answered Oct 25, 2017 at 9:16
Bruce TongBruce Tong
1,25813 silver badges13 bronze badges
Follow these steps to get UTC time of any timezone set in user's local system [This will be required for web applications to save different timezones to UTC]:
Javascript [client-side]:
var dateVar = new Date[]; var offset = dateVar.getTimezoneOffset[]; //getTimezoneOffset - returns the timezone difference between UTC and Local Time document.cookie = "offset="+offset;
Php [server-side]:
public function convert_utc_time[$date] { $time_difference = isset[$_COOKIE['offset']]?$_COOKIE['offset']:''; if[$time_difference != '']{ $time = strtotime[$date]; $time = $time + [$time_difference*60]; //minutes * 60 seconds $date = date["Y-m-d H:i:s", $time]; } //on failure of js, default timezone is set as UTC below return $date; } .. .. //in my function $timezone = 'UTC'; $date = $this->convert_utc_time[$post_date]; //$post_date['Y-m-d H:i:s'] echo strtotime[$date. ' '. $timezone]
answered Nov 9, 2018 at 10:16
Pradeep KumarPradeep Kumar
3,8952 gold badges33 silver badges39 bronze badges