Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get UTC time in PHP

Tags:

php

datetime

utc

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.

like image 445
Robin Michael Poothurai Avatar asked Dec 28 '11 11:12

Robin Michael Poothurai


People also ask

How can I get UTC in PHP?

PHP DateTime class provides an easy way to convert a date time stamp to UTC. You can convert any timezone to UTC DateTime using PHP. In the example code snippet, we will show you how to convert local date&time to UTC DateTime (YYYY-MM-DD HH:MM:SS format) in PHP.

How to get GMT time using PHP?

PHP | gmdate() Function The gmdate() is an inbuilt function in PHP which is used to format a GMT/UTC date and time and return the formatted date strings. It is similar to the date() function but it returns the time in Greenwich Mean Time (GMT).

What does gmdate do?

Definition and Usage The gmdate() function formats a GMT/UTC date and time, and returns the formatted date string.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

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

like image 138
nikc.org Avatar answered Sep 27 '22 20:09

nikc.org


A simple gmdate() will suffice

<?php print gmdate("Y-m-d\TH:i:s\Z"); 
like image 31
Shankar Narayana Damodaran Avatar answered Sep 27 '22 21:09

Shankar Narayana Damodaran