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.
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.
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).
Definition and Usage The gmdate() function formats a GMT/UTC date and time, and returns the formatted date string.
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.
Using gmdate
will always return a GMT date. Syntax is same as for date
.
A simple gmdate() will suffice
<?php print gmdate("Y-m-d\TH:i:s\Z");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With