Reputation: 5664
How can I get UTC/GMT +/- time stamp using PHP's date() function? For example, if I try
date("Y-m-d H:i:s", time());
I will get Unix time stamp; but I need to get UTC/GMT time stamp with string GMT/UTC+/-0400 or GMT/UTC+/-1000 based on local timings.
Upvotes: 158
Views: 372844
Reputation: 783
You want the actual timestamp (a number) If you want to return the numeric timestamp for UTC then this is my understanding of it. The getTimestamp() function is pretty useful.
$ts = new DateTime('NOW', new DateTimeZone('UTC')); // This could say UTC or GMT
echo $ts->getTimestamp();
I am going to try and provide a little bit of information as a starting point for anyone that is confused by GMT/BST/UTC/DST/WET/CET/EET but this is my interpretation of what I understand - pretty sure it's accurate as far as speaking in layman's terms.
What is a timestamp? A timestamp is a numeric equivalent of the number of seconds since 1st January 1970. Essentially, the point at which the time in Greenwich UK was adopted as the global standard/base-line that all countries agreed to adopt. So because it was adopted at a place in the UK, it just happens that GMT is the same as UTC.
GMT vs UTC vs DST vs BST vs WET vs CET vs EET Arggh! Okay, so the important bit to understand is that GMT & UTC are the same because they started in the UK London->Greenwich's winter as the origin. Essentially, UTC = GMT+00:00.
Then, at a later time UTC was adopted as the best way to refer to the start of this agreed 1st January 1970 rule to avoid confusing it with the time for UK and similar time zone countries. UTC is the standard. GMT is the time zone for a country/countries.
The bit that gets confusing is when there is Daylight Saving Time (DST). Now, Britain calls this BST (British Summer Time) which is basically a shortcut for saying GMT+01:00. Europe (as an example) acknowledge DayLight Saving Time too but when they refer to the change they don't call it BST (British Summer Time) the name it based on the European Timezone they are in.
BTW, if you wanted to know if you're current Time zone is in Daylight Saving Time you can do:-
$d = date("I");
This returns 1 for "Yes I am" or "0" for "No I am not"
There are three European Timezones. There is the Western European Timezone (WET) which is basically the UK and some other countries because of their alignment on the globe when it spins matches up with GMT.
Then there is Central European time which is where most European countries sit (UTC+01:00) and then Easter European time is (UTC+02:00)
So with that in mind, when you take into account DST (Daylight Saving Time) you can assume that each one of those countries that acknowledges it will update their current standard time by 1 hour.
So UK for example will become GMT+01:00 (because we like to call it GMT) and the shortcut is BST (British Summer Time)
France would call Britain's Summer Time as UTC+01:00 or WET+01:00.
Daylight Saving Time to France is called CEST (Central European Summer Time) which is UTC+02:00 or CET+1:00 (because CET is already known as UTC+01:00)
And with Eastern European Time - you get the idea...
I hope that helps a little bit but obviously, you need to do your own research. This link has a good graphic showing the zones and more info. https://en.wikipedia.org/wiki/Central_European_Time
Upvotes: 1
Reputation: 89
You'd like to use gmdate method to get GMT date.
gmdate("Y-m-d H:i:s");
gmdate date-formats resemble those in the date() method.
Upvotes: 7
Reputation: 195
Below find the PHP code to get current UTC(Coordinated Universal Time) time
<?php
// Prints the day
echo gmdate("l") . "<br>";
// Prints the day, date, month, year, time, AM or PM
echo gmdate("l jS \of F Y h:i:s A");
?>
Upvotes: -1
Reputation: 2888
Obtaining UTC date
gmdate("Y-m-d H:i:s");
Obtaining UTC timestamp
time();
The result will not be different even you have date_default_timezone_set
on your code.
Upvotes: 28
Reputation: 12460
As previously answered here, since PHP 5.2.0 you can use the DateTime
class and specify the UTC timezone with an instance of DateTimeZone
.
The DateTime __construct() documentation suggests passing "now" as the first parameter when creating a DateTime instance and specifying a timezone to get the current time.
$date_utc = new \DateTime("now", new \DateTimeZone("UTC"));
echo $date_utc->format(\DateTime::RFC850); # Saturday, 18-Apr-15 03:23:46 UTC
Upvotes: 75
Reputation: 40861
with string GMT/UTC +/-0400 or GMT/UTC +/-1000 based on local timings
Your custom format is just missing O
to give you the timezone offsets from local time.
Difference to Greenwich time (GMT) in hours Example: +0200
date_default_timezone_set('America/La_Paz');
echo date('Y-m-d H:i:s O');
2018-01-12 12:10:11 -0400
However, for maximized portability/interoperability, I would recommend using the ISO8601 date format c
date_default_timezone_set('America/La_Paz');
echo date('c');
2018-01-12T12:10:11-04:00
date_default_timezone_set('Australia/Brisbane');
echo date('c');
2018-01-13T02:10:11+10:00
You can use also gmdate
and the timezone offset string will always be +00:00
date_default_timezone_set('America/La_Paz');
echo gmdate('c');
2018-01-12T16:10:11+00:00
date_default_timezone_set('Australia/Brisbane');
echo gmdate('c');
2018-01-12T16:10:11+00:00
Upvotes: 5
Reputation: 2088
/**
* Converts a local Unix timestamp to GMT
*
* @param int Unix timestamp
* @return int
*/
function local_to_gmt($time = '')
{
if ($time === '')
{
$time = time();
}
return mktime(
gmdate('G', $time),
gmdate('i', $time),
gmdate('s', $time),
gmdate('n', $time),
gmdate('j', $time),
gmdate('Y', $time)
);
}
Upvotes: 0
Reputation: 126
You can use following to get UTC time:
date_default_timezone_set('Asia/Calcutta');
$current_date = date("Y/m/d g:i A");
$ist_date = DateTime::createFromFormat(
'"Y/m/d g:i A"',
$current_date,
new DateTimeZone('Asia/Calcutta')
);
$utc_date = clone $ist_date;
$utc_date->setTimeZone(new DateTimeZone('UTC'));
echo 'UTC: ' . $utc_date->format('Y-m-d g:i A');
Upvotes: 1
Reputation: 68466
A simple gmdate() will suffice
<?php
print gmdate("Y-m-d\TH:i:s\Z");
Upvotes: 123
Reputation: 4048
You can use gmmktime function without arguments to obtain the current UTC timestamp:
$time = gmmktime();
echo date("Y-m-d H:i:s", $time);
gmmktime will only work if your server time is using UTC. For example, my server is set to US/Pacific. the listed function above echos back Pacific time.
Upvotes: 6
Reputation: 100175
$time = time();
$check = $time+date("Z",$time);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $check);
Upvotes: 35
Reputation: 784958
Other than calling gmdate
you can also put this code before your rest of the code:
<?php
date_default_timezone_set("UTC");
?>
That will make rest of your date/time related calls to use GMT/UTC timezone.
Upvotes: 14