set_formatted_gmt_time

Sets a variable to the current GMT time formatted according to a specified format string.

Syntaxset_formatted_gmt_time variable format_string;
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments2

Description

The set_formatted_gmt_time directive is part of the NGINX Set-Misc module and allows users to set a specified variable to the current GMT (Greenwich Mean Time) time formatted according to a format string provided by the user. The required parameters include a format string which determines how the date and time will be displayed, and the variable to be set with the formatted GMT time. This is especially useful in situations where timestamps need to be logged or displayed in a specific format for APIs or frontend applications.

The directive can be used in different contexts such as http, server, location, and within if statements in these contexts. The flexibility of its usage means that it can easily be integrated into various configurations, allowing for dynamic timestamp generation. The format string syntax adheres to the same conventions used in strftime, giving users control over the styling of their output (e.g., %Y-%m-%d %H:%M:%S for a standard date-time format).

Config Example

set_formatted_gmt_time $current_time "%Y-%m-%d %H:%M:%S";

Ensure the variable is not already set to another value before using this directive, as it will override the previous value.

The format string must adhere to the correct strftime format to avoid unintended results.

← Back to all directives