Current Location: Home> Latest Articles> PHP Date Formats Explained: Timestamps, ISO 8601, and Custom Formats

PHP Date Formats Explained: Timestamps, ISO 8601, and Custom Formats

M66 2025-10-25

Overview of PHP Date Formats

PHP provides multiple ways to handle and represent dates and times, including timestamps, Unix timestamps, and the ISO 8601 standard format. PHP also offers a range of date functions, such as strtotime(), date(), and strftime(), which allow converting date strings into different formats.

Timestamps

A timestamp is an integer representing the number of seconds since the epoch. It is a universal way to store and transfer date and time values.

Unix Timestamps

Unix timestamps represent the number of seconds elapsed since January 1, 1970, 00:00:00 UTC. This widely used format can be easily converted to other formats.

$timestamp = time();

ISO 8601 Dates and Times

ISO 8601 is an international standard for representing dates and times. Common formats include:

  • Date: YYYY-MM-DD, e.g., 2023-04-10
  • Time: HH:MM:SS, e.g., 15:30:15
  • Date and Time: YYYY-MM-DDTHH:MM:SS, e.g., 2023-04-10T15:30:15

PHP Date Function Formats

PHP provides several functions for handling date strings and timestamps:

  • strtotime(): Converts a string into a timestamp, e.g.,
    $timestamp = strtotime("2023-04-10 15:30:15");
  • date(): Converts a timestamp or date string into a specific format, e.g.,
    $date = date("Y-m-d");
  • strftime(): Similar to date(), but offers a wider range of formatting options, e.g.,
    $date = strftime("%Y-%m-%dT%H:%M:%S");

Custom Date Formats

PHP allows you to define custom date formats, usually using the DateTime::format() method. Common format symbols include:

  • %Y: Four-digit year
  • %m: Two-digit month
  • %d: Two-digit day
  • %H: Two-digit hour (0-23)
  • %M: Two-digit minute
  • %S: Two-digit second

For example, the following code generates "2023-04-10 15:30:15":

$date = date("Y-m-d H:i:s");

With these methods, you can flexibly handle various PHP date formats to meet different requirements.