简体   繁体   中英

Get current UTC string in PHP

Is there a way in PHP to calculate the current UTC string? Sometihng that would throw current UTC timestring format on each different server.

In europer it would throw "UTC +1" ... or "UTC 0" ... , "UTC +12:00" always depending on current server settings.

Return UTC timestring on different servers:

$dt = new DateTime('now', new DateTimezone('UTC'));
# or
$dt = new DateTime('UTC');

echo $dt->format('Y-m-d H:i:s');

If you have DateTime objects in different timezones, you can always get timezone offset by calling DateTime::getOffset() who will return offset in seconds; or just use format you need:

$dt = new DateTime('now', new DateTimeZone('America/New_York'));
echo $dt->getOffset(), " : ", $dt->format('O / P / e / T / Z');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM