Date Function#
The PHP date() function is used to format a date and time according to a specified format.
##Syntax
date(format, timestamp)
Parameters#
format(required)The
formatparameter is a string that specifies how the outputted date should be formatted.It uses a series of format characters, where each character represents a different aspect of the date/time.
Common format characters include:
Y: 4-digit year (e.g., 2024)y: 2-digit year (e.g., 24)m: 2-digit month (01 to 12)n: Month without leading zeros (1 to 12)d: 2-digit day of the month (01 to 31)j: Day of the month without leading zeros (1 to 31)H: 2-digit hour in 24-hour format (00 to 23)i: 2-digit minutes (00 to 59)s: 2-digit seconds (00 to 59)l: Full day of the week (e.g., Monday)D: Abbreviated day of the week (e.g., Mon)F: Full month name (e.g., October)M: Abbreviated month name (e.g., Oct)
timestamp(optional)The
timestampis an optional integer parameter that specifies a Unix timestamp, representing the number of seconds since January 1, 1970.If omitted, the function uses the current date and time.
Example:
date("Y-m-d", 1672531199)would output the date corresponding to the given timestamp.
Examples#
Get the current date
echo date("Y-m-d"); // Outputs: 2024-10-30Format a specific timestamp
$timestamp = 1672531199; echo date("Y-m-d H:i:s", $timestamp); // Outputs: 2023-01-01 00:59:59
The date() function is commonly used for formatting dates in various formats, making it versatile for generating date strings for display, logging, or processing purposes.