How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
The gmdate() function in PHP is designed exactly for this purpose: it formats a timestamp according to the GMT timezone, rather than your server's local time. However, the snippet you attempted fails because of a mismatch between the format string syntax and the actual characters supported by the function. Specifically, using literal text like 'yyyy-mm-dd' inside the format string does not work as intended; gmdate() interprets these letters as format specifiers rather than literal values to display.
Furthermore, there is a critical detail regarding timezone handling that often trips up developers. The second argument to gmdate(), which you passed as time(), returns the current Unix timestamp in your server's local context. While gmdate() ignores this specific argument when calculating the display time (it always assumes UTC), it still uses the provided timestamp value for its calculation. If your intention is strictly to display the current date and time in GMT, passing time() works, but it can be conceptually confusing because you are mixing local context with a global output. It is often cleaner to explicitly use strtotime('now') or rely on the fact that gmdate() effectively treats the input as if it were UTC midnight of the current day when no specific offset is applied in older PHP versions, though modern PHP handles this robustly by ignoring the second argument entirely for the timezone calculation but using it for the raw time value.
The primary issue with your outputâ13131313-1111-2323 0707:1111:3131âstems from how PHP parses invalid format characters. In standard date() and gmdate() functions, letters like y, m, d, H, i, and s represent year, month, day, hour, minute, and second respectively. However, you cannot use uppercase Y for a 4-digit year if you want it to behave exactly like yyyy in some libraries, but more importantly, you cannot use literal strings like 'yyyy'. PHP expects the format string to consist of recognized tokens. If a character is not recognized, or if the syntax is malformed, the function may return unexpected results or throw warnings depending on the configuration.
To achieve the specific format yyyy-mm-dd hh:mm:ss in GMT, you must construct the format string using only valid tokens. The correct format string is 'Y-m-d H:i:s'. Here, Y outputs a 4-digit year, - and : are literal characters that appear as-is in the output, and m, d, H, i, s represent month, day, hour (24-hour format), minute, and second.
Here is how you should implement this:
$gmtDate = gmdate('Y-m-d H:i:s');
echo $gmtDate; // Example output: 2023-10-27 14:30:45
Notice that we did not pass time() as the second argument. While passing time() is valid, it serves no functional purpose here because gmdate() always treats the time as GMT regardless of the input value's origin; it simply takes the Unix timestamp provided and converts it to a human-readable string in UTC. If you were to pass a local timestamp, gmdate would still display it in UTC, which might be confusing if you expect it to reflect the local time converted to UTC. By omitting the second argument, you tell PHP to use the current time directly.
If you are working within the Laravel framework, you might prefer using the Carbon library for more readable intent, although native PHP functions are sufficient for this specific task. You could write:
use Carbon\Carbon;
$gmtDate = Carbon::now()->timezone('UTC')->format('Y-m-d H:i:s');
This approach explicitly sets the timezone to UTC before formatting, removing any ambiguity about whether the time being displayed is local or global. This level of explicitness is valuable in large codebases where team members might have different expectations regarding default timezones.
A common pitfall when working with dates in web applications is accidentally displaying local time when GMT is required for logging or API responses. Always verify your output by comparing it against a known UTC source, such as date -u on a Linux terminal. If you find yourself needing to manipulate dates frequently, consider sticking to the native DateTime class or Carbon, which offer better readability and fewer edge-case bugs compared to string parsing functions. For instance, using gmdate() is perfectly fine for simple formatting needs, but if your application requires timezone-aware objects that persist through multiple operations, transitioning to an object-oriented approach prevents subtle bugs related to daylight saving time changes or server clock drift.
