In PHP, the date() function is used to retrieve localized timestamps. However, to obtain UTC time stamps with specific offsets, a different approach is required.
To retrieve UTC time stamps in PHP, you can use the gmdate() function. The syntax is similar to that of date(), but it always returns timestamps in UTC. For example:
<code class="php">$timestamp = gmdate("Y-m-d H:i:s"); // Returns UTC timestamp</code>
The gmdate() function allows you to use the same formatting specifiers as date(), so you can easily format the timestamp to a desired string.
To add a GMT/UTC offset to the timestamp, you can use the following string format:
GMT/UTC+/-####
where #### represents the offset in minutes. For example, to add an offset of 4 hours (GMT 0400), you would use the following format:
GMT/UTC+0400
You can then append this offset string to the gmdate() result using the . operator:
<code class="php">$timestamp_with_offset = gmdate("Y-m-d H:i:s") . " GMT/UTC+0400";</code>
This will output a timestamp in UTC with the specified offset.
The above is the detailed content of How to Obtain UTC Time Stamps with Specific Offsets in PHP?. For more information, please follow other related articles on the PHP Chinese website!