[php] get UTC time in PHP

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.

This question is related to php datetime utc

The answer is


Below find the PHP code to get current UTC(Coordinated Universal Time) time

_x000D_
_x000D_
<?php_x000D_
// Prints the day_x000D_
echo gmdate("l") . "<br>";_x000D_
_x000D_
// Prints the day, date, month, year, time, AM or PM_x000D_
echo gmdate("l jS \of F Y h:i:s A");_x000D_
?>
_x000D_
_x000D_
_x000D_


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


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.


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.


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

A simple gmdate() will suffice

<?php
print gmdate("Y-m-d\TH:i:s\Z");

$time = time();
$check = $time+date("Z",$time);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $check);

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.


date("Y-m-d H:i:s", time() - date("Z"))

/**
     * 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)
        );
    }

Using gmdate will always return a GMT date. Syntax is same as for date.


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');

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?

Examples related to utc

Convert LocalDateTime to LocalDateTime in UTC How to set the timezone in Django? Java Convert GMT/UTC to Local time doesn't work as expected Should MySQL have its timezone set to UTC? How to Convert UTC Date To Local time Zone in MySql Select Query How to convert UTC timestamp to device local time in android Convert python datetime to epoch with strftime Convert Java Date to UTC String How do I get a UTC Timestamp in JavaScript? Converting datetime.date to UTC timestamp in Python