This topic describes nine built-in time functions for reading timestamps, formatting time strings for HTTP and cookie headers, and converting between time representations.
| Function | Returns | Description |
|---|---|---|
today() | String | Current date in yyyy-mm-dd format (local time) |
time() | Integer | Current UNIX timestamp in seconds, without milliseconds |
now() | Float | Current UNIX timestamp in seconds, with milliseconds |
localtime() | String | Current date and time in yyyy-mm-dd hh:mm:ss format (local time) |
utctime() | String | Current date and time in yyyy-mm-dd hh:mm:ss format (UTC) |
cookie_time(sec) | String | Formats a UNIX timestamp as a cookie-compatible time string |
http_time(sec) | String | Formats a UNIX timestamp as an HTTP header time string |
parse_http_time(str) | Integer or false | Parses an HTTP time string and returns its UNIX timestamp |
unixtime(year, month, day, hour, min, sec) | Integer | Constructs a UNIX timestamp from date and time components |
today
str = today()Returns the current date as a string in yyyy-mm-dd format. This is the local time of the server.
Parameters: None
Example:
say(concat('today:', today()))Output:
today:2021-12-29time
secs = time()Returns the current UNIX timestamp as an integer (seconds since the Unix epoch), without the millisecond fractional part.
Parameters: None
Example:
say(concat('time:', time()))Output:
time:1559109666To get sub-second precision, use now() instead.now
secs = now()Returns the current UNIX timestamp as a floating-point number, including milliseconds as the decimal part.
Parameters: None
Example:
say(concat('now:', now()))Output:
now:1559109666.644localtime
str = localtime()Returns the current date and time as a string in yyyy-mm-dd hh:mm:ss format. This is the local time of the server.
Parameters: None
Example:
say(concat('localtime:', localtime()))Output:
localtime:2021-12-29 14:02:41utctime
str = utctime()Returns the current date and time as a string in yyyy-mm-dd hh:mm:ss format. This is UTC time.
Parameters: None
Example:
say(concat('utctime:', utctime()))Output:
utctime:2021-12-29 06:02:41cookie_time
str = cookie_time(sec)Formats a UNIX timestamp as a time string suitable for use in HTTP cookies.
Parameters:
| Parameter | Type | Description |
|---|---|---|
sec | Integer | A UNIX timestamp in seconds. Call time() to get the current timestamp. |
Example:
say(concat('cookie_time:', cookie_time(time())))Output:
cookie_time:Wed, 29-Dec-21 06:02:41 GMThttp_time
str = http_time(sec)Formats a UNIX timestamp as a time string suitable for use in HTTP headers such as Last-Modified.
Parameters:
| Parameter | Type | Description |
|---|---|---|
sec | Integer | A UNIX timestamp in seconds. Call time() to get the current timestamp. |
Example:
say(concat('http_time:', http_time(time())))Output:
http_time:Wed, 29 Dec 2021 06:02:41 GMTcookie_timeandhttp_timeproduce different formats from the same timestamp.cookie_timeuses dashes and a two-digit year (29-Dec-21), whilehttp_timeuses spaces and a four-digit year (29 Dec 2021).
parse_http_time
sec = parse_http_time(str)Parses an HTTP-formatted time string and returns the corresponding UNIX timestamp.
Parameters:
| Parameter | Type | Description |
|---|---|---|
str | String | An HTTP-formatted time string such as Thu, 22-Dec-10 10:20:35 GMT. Call http_time() to generate a valid input. |
Return value: The UNIX timestamp as an integer if parsing succeeds, or false if the input string is malformed.
Example:
say(concat('parse_http_time:', parse_http_time(http_time(time()))))Output:
parse_http_time:1559109761unixtime
secs = unixtime(year, month, day, hour, min, sec)Constructs a UNIX timestamp from the specified date and time components.
Parameters:
| Parameter | Type | Description |
|---|---|---|
year | Integer | Four-digit year |
month | Integer | Month |
day | Integer | Day of the month |
hour | Integer | Hour |
min | Integer | Minute |
sec | Integer | Second |
Return value: The UNIX timestamp corresponding to the specified date and time.
Example:
t = UNIXtime(1970, 1, 1, 8, 0, 0)
say(concat('UNIXtime()=', t))Output:
UNIXtime()=0