How to Get Greenwich Mean Time in PHP

How do I get Greenwich Mean Time in PHP?

No matter in which GMT time zone the server is, here is an extremely easy way to get time and date for any time zone. This is done with the time() and gmdate() functions. The gmdate() function normally gives us GMT time, but by doing a trick with the time() function we can get GMT+N or GMT-N, meaning we can get the time for any GMT time zone.

For example, if you have to get the time for GMT+5, you can do it as follows

<?php 
$offset=5*60*60; //converting 5 hours to seconds.
$dateFormat="d-m-Y H:i";
$timeNdate=gmdate($dateFormat, time()+$offset);
?>

Now if you have to get the time for GMT-5, you can just subtract the offset from the time() instead of adding to it, like in the following example where we are getting the time for GMT-4

<?php 
$offset=4*60*60; //converting 4 hours to seconds.
$dateFormat="d-m-Y H:i"; //set the date format
$timeNdate=gmdate($dateFormat, time()-$offset); //get GMT date - 4
?>

PHP get current local time based on GMT offset

You can only exclusively use GMT offsets for right now, i.e. it's completely useless if the user told you his GMT offset half a year ago and you want to know now what time it is at his location. It could basically be anything within a range of a few hours. Even if you got the offset a few seconds ago, it may already be different by the time you do the calculations if you happen to hit a DST change exactly.

Having said that, you'll want to add/subtract the offset hours from the formatted time, which is really the only thing you can do.

$offset = 5;
$hours = (gmdate('H') + $offset) % 24;

echo $hours . gmdate(':i:s Y m d');

That's just an example, you'll need to handle minute offsets as well.

PHP - Correct way to get current UTC date time

I am not sure where the examples you have found come from, but gmdate is almost certainly the best way to get the correct GMT date and time from the current time, or a supplied timestamp.

From the PHP docs:

Identical to the date() function except that the time returned is
Greenwich Mean Time (GMT).

Usage to get the current GMT date time:

echo gmdate("Y/m/d H:i:s");

Usage with an existing timestamp:

echo gmdate("Y/m/d H:i:s", $timestamp);

The date value supplied is automatically adjusted from the local timezone to GMT.

Obviously, the format of the date can be changed to whatever you wish by modifying the first parameter.

Display GMT time zone in php

<?php
// This will get an array of the timezones for Australia
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, "AU");
// Using the last element, which is 'Australia/Sydney'.
$timezone = new DateTimeZone(end($timezones));
$datetime = new DateTime('now', $timezone);
echo end($timezones). ' GMT ' . $datetime->format('P'); // +11:00
?>

This will output: Australia/Sydney GMT +11:00

The following will iterate every timezone for the countries within the array.

<?php

$countries = array( 'AF' => 'AFGHANISTAN', 'AL'=>'ALBANIA', 'DZ'=>'ALGERIA', 'AS'=>'AMERICAN SAMOA', 'AD'=>'ANDORRA', 'AO'=>'ANGOLA', 'AI'=>'ANGUILLA', 'AQ'=>'ANTARCTICA', 'AG'=>'ANTIGUA AND BARBUDA', 'AR'=>'ARGENTINA', 'AM'=>'ARMENIA', 'AW'=>'ARUBA', 'AU'=>'AUSTRALIA', 'AT'=>'AUSTRIA', 'AZ'=>'AZERBAIJAN', 'BS'=>'BAHAMAS', 'BH'=>'BAHRAIN', 'BD'=>'BANGLADESH', 'BB'=>'BARBADOS', 'BY'=>'BELARUS', 'BE'=>'BELGIUM', 'BZ'=>'BELIZE', 'BJ'=>'BENIN', 'BM'=>'BERMUDA', 'BT'=>'BHUTAN', 'BO'=>'BOLIVIA', 'BA'=>'BOSNIA AND HERZEGOVINA', 'BW'=>'BOTSWANA', 'BV'=>'BOUVET ISLAND', 'BR'=>'BRAZIL', 'IO'=>'BRITISH INDIAN OCEAN TERRITORY', 'BN'=>'BRUNEI DARUSSALAM', 'BG'=>'BULGARIA', 'BF'=>'BURKINA FASO', 'BI'=>'BURUNDI', 'KH'=>'CAMBODIA', 'CM'=>'CAMEROON', 'CA'=>'CANADA', 'CV'=>'CAPE VERDE', 'KY'=>'CAYMAN ISLANDS', 'CF'=>'CENTRAL AFRICAN REPUBLIC', 'TD'=>'CHAD', 'CL'=>'CHILE', 'CN'=>'CHINA', 'CX'=>'CHRISTMAS ISLAND', 'CC'=>'COCOS (KEELING) ISLANDS', 'CO'=>'COLOMBIA', 'KM'=>'COMOROS', 'CG'=>'CONGO', 'CD'=>'CONGO, THE DEMOCRATIC REPUBLIC OF THE', 'CK'=>'COOK ISLANDS', 'CR'=>'COSTA RICA', 'CI'=>'COTE D IVOIRE', 'HR'=>'CROATIA', 'CU'=>'CUBA', 'CY'=>'CYPRUS', 'CZ'=>'CZECH REPUBLIC', 'DK'=>'DENMARK', 'DJ'=>'DJIBOUTI', 'DM'=>'DOMINICA', 'DO'=>'DOMINICAN REPUBLIC', 'TP'=>'EAST TIMOR', 'EC'=>'ECUADOR', 'EG'=>'EGYPT', 'SV'=>'EL SALVADOR', 'GQ'=>'EQUATORIAL GUINEA', 'ER'=>'ERITREA', 'EE'=>'ESTONIA', 'ET'=>'ETHIOPIA', 'FK'=>'FALKLAND ISLANDS (MALVINAS)', 'FO'=>'FAROE ISLANDS', 'FJ'=>'FIJI', 'FI'=>'FINLAND', 'FR'=>'FRANCE', 'GF'=>'FRENCH GUIANA', 'PF'=>'FRENCH POLYNESIA', 'TF'=>'FRENCH SOUTHERN TERRITORIES', 'GA'=>'GABON', 'GM'=>'GAMBIA', 'GE'=>'GEORGIA', 'DE'=>'GERMANY', 'GH'=>'GHANA', 'GI'=>'GIBRALTAR', 'GR'=>'GREECE', 'GL'=>'GREENLAND', 'GD'=>'GRENADA', 'GP'=>'GUADELOUPE', 'GU'=>'GUAM', 'GT'=>'GUATEMALA', 'GN'=>'GUINEA', 'GW'=>'GUINEA-BISSAU', 'GY'=>'GUYANA', 'HT'=>'HAITI', 'HM'=>'HEARD ISLAND AND MCDONALD ISLANDS', 'VA'=>'HOLY SEE (VATICAN CITY STATE)', 'HN'=>'HONDURAS', 'HK'=>'HONG KONG', 'HU'=>'HUNGARY', 'IS'=>'ICELAND', 'IN'=>'INDIA', 'ID'=>'INDONESIA', 'IR'=>'IRAN, ISLAMIC REPUBLIC OF', 'IQ'=>'IRAQ', 'IE'=>'IRELAND', 'IL'=>'ISRAEL', 'IT'=>'ITALY', 'JM'=>'JAMAICA', 'JP'=>'JAPAN', 'JO'=>'JORDAN', 'KZ'=>'KAZAKSTAN', 'KE'=>'KENYA', 'KI'=>'KIRIBATI', 'KP'=>'KOREA DEMOCRATIC PEOPLES REPUBLIC OF', 'KR'=>'KOREA REPUBLIC OF', 'KW'=>'KUWAIT', 'KG'=>'KYRGYZSTAN', 'LA'=>'LAO PEOPLES DEMOCRATIC REPUBLIC', 'LV'=>'LATVIA', 'LB'=>'LEBANON', 'LS'=>'LESOTHO', 'LR'=>'LIBERIA', 'LY'=>'LIBYAN ARAB JAMAHIRIYA', 'LI'=>'LIECHTENSTEIN', 'LT'=>'LITHUANIA', 'LU'=>'LUXEMBOURG', 'MO'=>'MACAU', 'MK'=>'MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF', 'MG'=>'MADAGASCAR', 'MW'=>'MALAWI', 'MY'=>'MALAYSIA', 'MV'=>'MALDIVES', 'ML'=>'MALI', 'MT'=>'MALTA', 'MH'=>'MARSHALL ISLANDS', 'MQ'=>'MARTINIQUE', 'MR'=>'MAURITANIA', 'MU'=>'MAURITIUS', 'YT'=>'MAYOTTE', 'MX'=>'MEXICO', 'FM'=>'MICRONESIA, FEDERATED STATES OF', 'MD'=>'MOLDOVA, REPUBLIC OF', 'MC'=>'MONACO', 'MN'=>'MONGOLIA', 'MS'=>'MONTSERRAT', 'MA'=>'MOROCCO', 'MZ'=>'MOZAMBIQUE', 'MM'=>'MYANMAR', 'NA'=>'NAMIBIA', 'NR'=>'NAURU', 'NP'=>'NEPAL', 'NL'=>'NETHERLANDS', 'AN'=>'NETHERLANDS ANTILLES', 'NC'=>'NEW CALEDONIA', 'NZ'=>'NEW ZEALAND', 'NI'=>'NICARAGUA', 'NE'=>'NIGER', 'NG'=>'NIGERIA', 'NU'=>'NIUE', 'NF'=>'NORFOLK ISLAND', 'MP'=>'NORTHERN MARIANA ISLANDS', 'NO'=>'NORWAY', 'OM'=>'OMAN', 'PK'=>'PAKISTAN', 'PW'=>'PALAU', 'PS'=>'PALESTINIAN TERRITORY, OCCUPIED', 'PA'=>'PANAMA', 'PG'=>'PAPUA NEW GUINEA', 'PY'=>'PARAGUAY', 'PE'=>'PERU', 'PH'=>'PHILIPPINES', 'PN'=>'PITCAIRN', 'PL'=>'POLAND', 'PT'=>'PORTUGAL', 'PR'=>'PUERTO RICO', 'QA'=>'QATAR', 'RE'=>'REUNION', 'RO'=>'ROMANIA', 'RU'=>'RUSSIAN FEDERATION', 'RW'=>'RWANDA', 'SH'=>'SAINT HELENA', 'KN'=>'SAINT KITTS AND NEVIS', 'LC'=>'SAINT LUCIA', 'PM'=>'SAINT PIERRE AND MIQUELON', 'VC'=>'SAINT VINCENT AND THE GRENADINES', 'WS'=>'SAMOA', 'SM'=>'SAN MARINO', 'ST'=>'SAO TOME AND PRINCIPE', 'SA'=>'SAUDI ARABIA', 'SN'=>'SENEGAL', 'SC'=>'SEYCHELLES', 'SL'=>'SIERRA LEONE', 'SG'=>'SINGAPORE', 'SK'=>'SLOVAKIA', 'SI'=>'SLOVENIA', 'SB'=>'SOLOMON ISLANDS', 'SO'=>'SOMALIA', 'ZA'=>'SOUTH AFRICA', 'GS'=>'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS', 'ES'=>'SPAIN', 'LK'=>'SRI LANKA', 'SD'=>'SUDAN', 'SR'=>'SURINAME', 'SJ'=>'SVALBARD AND JAN MAYEN', 'SZ'=>'SWAZILAND', 'SE'=>'SWEDEN', 'CH'=>'SWITZERLAND', 'SY'=>'SYRIAN ARAB REPUBLIC', 'TW'=>'TAIWAN, PROVINCE OF CHINA', 'TJ'=>'TAJIKISTAN', 'TZ'=>'TANZANIA, UNITED REPUBLIC OF', 'TH'=>'THAILAND', 'TG'=>'TOGO', 'TK'=>'TOKELAU', 'TO'=>'TONGA', 'TT'=>'TRINIDAD AND TOBAGO', 'TN'=>'TUNISIA', 'TR'=>'TURKEY', 'TM'=>'TURKMENISTAN', 'TC'=>'TURKS AND CAICOS ISLANDS', 'TV'=>'TUVALU', 'UG'=>'UGANDA', 'UA'=>'UKRAINE', 'AE'=>'UNITED ARAB EMIRATES', 'GB'=>'UNITED KINGDOM', 'US'=>'UNITED STATES', 'UM'=>'UNITED STATES MINOR OUTLYING ISLANDS', 'UY'=>'URUGUAY', 'UZ'=>'UZBEKISTAN', 'VU'=>'VANUATU', 'VE'=>'VENEZUELA', 'VN'=>'VIET NAM', 'VG'=>'VIRGIN ISLANDS, BRITISH', 'VI'=>'VIRGIN ISLANDS, U.S.', 'WF'=>'WALLIS AND FUTUNA', 'EH'=>'WESTERN SAHARA', 'YE'=>'YEMEN', 'YU'=>'YUGOSLAVIA', 'ZM'=>'ZAMBIA', 'ZW'=>'ZIMBABWE', );

foreach ($countries as $code => $country) {
$timezones = DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $code);

foreach ($timezones as $timezone_name) {
$timezone = new DateTimeZone($timezone_name);
$datetime = new DateTime('now', $timezone);
echo $country. ' GMT ' . $datetime->format('P') . ' ' . $timezone_name . '<br />';
}

}
?>

Convert time in PHP from gmt offset

$date = DateTime::createFromFormat('H:i P', '11:00 +02:00');
$date->setTimeZone(new DateTimeZone('GMT'));
echo $date->format('H:i');

Demo

This:

  1. Creates a DateTime object with the time and offset
  2. Converts the timezone to GMT
  3. Echo's out the time in GMT

how to convert php date formats to GMT and vice versa?

Although the gmdate functions are available. If you are using PHP 5.2 or greater, then consider using the DateTime object.

Here's code to switch to GMT

$date = new DateTime();
$date->setTimezone(new DateTimeZone('GMT'));

and back to the default timezone...

$date = new DateTime('2011-01-01', new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone(date_default_timezone_get()));

Using the DateTime object lets your create a datetime, just like the procedural functions, except that you keep a reference to an instance.

e.g.

// Get a reference to Christmas of 2011, at lunch time.
$date = new DateTime('2011-12-25 13:00:00');

// Print the date for people to see, in whatever format we specify.
echo $date->format('D jS M y');

// Change the timezone to GMT.
$date->setTimezone(new DateTimeZone('GMT'));

// Now print the date/time it would in the GMT timezone
// as opposed to the default timezone it was created with.
echo $date->format('Y-m-d H:i:s');

// Just to show of some more, get the previous Sunday
$date->modify('previous Sunday');

There's a whole lot of functions you can use, that are much more readable that the procedural functions.


Explicit example of converting from a timezone to GMT

$melbourne = new DateTimeZone('Australia/Melbourne');
$gmt = new DateTimeZone('GMT');

$date = new DateTime('2011-12-25 00:00:00', $melbourne);
$date->setTimezone($gmt);
echo $date->format('Y-m-d H:i:s');
// Output: 2011-12-24 13:00:00
// At midnight on Christmas eve in Melbourne it will be 1pm on Christmas Eve GMT.

echo '<br/>';

// Convert it back to Australia/Melbourne
$date->setTimezone($melbourne);
echo $date->format('Y-m-d H:i:s');

Using your Asia/Kolkata to America/New_York

date_default_timezone_set('Asia/Kolkata');
$date = new DateTime('2011-03-28 13:00:00');
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format("Y-m-d H:i:s");
//Outputs: 2011-03-28 03:30:00

How to convert different time zone date & time to GMT time in php

The gmdate() function will output the date/time in GMT and the strtotime() function will convert your datetime strings to a valid parameter for gmdate().

$date_format1 = "Sat, 31 May 2014 00:00:00 +0200";
$date_format2 = "Mon, 02 Jun 2014 14:00:00 -0400";
$date_format3 = "Mon, 02 Jun 2014 11:03:00 BST";
$date_format4 = "Mon, 02 Jun 2014 10:03:00 EDT";

$newFormat = 'd/m/Y H:i:s';

echo gmdate($newFormat, strtotime($date_format1)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format2)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format3)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format4)) . PHP_EOL;

And the results would be:-

30/05/2014 22:00:00
02/06/2014 18:00:00
02/06/2014 10:03:00
02/06/2014 14:03:00

PHP get UTC/GMT time, round to nearest minute, and format to yyyyMMddHHmm

I suggest you use a DateTime object instead. Handling dates (and times) can be very difficult if you want to do it correctly, and PHP already makes it pretty easy for you this way.

Then, just add one minute if the "seconds hand" is at least at 30:

$dateTime = new DateTime();
$dateTime->setTimezone(new DateTimeZone('UTC'));
echo 'Debug date: ' . $dateTime->format('Y-m-d H:i:s') . PHP_EOL;
echo 'Rounded to minute: ';

if ($dateTime->format("s") >= 30) {
$dateTime->add(new DateInterval('PT1M')); // adds one minute to current time
}

echo $dateTime->format("YmdHi") . PHP_EOL;

Example outputs:

Debug date: 2021-03-18 23:57:25
Rounded to minute: 202103182357

Debug date: 2021-03-18 23:57:38
Rounded to minute: 202103182358

Debug date: 2021-03-18 23:59:34
Rounded to minute: 202103190000

This also takes care of overlapping days and such (see last example above), which fiddling around with the raw numbers would not - or at least it would get very complicated that way.

Php Convert time in gmt format

PHP's DateTime Object is a good choice:

$GMT = new DateTimeZone("GMT");
$date = new DateTime( "2011-01-01 15:00:00", $GMT );
$date->setTimezone( $newTZ );
echo $date->format('Y-m-d H:i:s');


Related Topics



Leave a reply



Submit