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.
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 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 is an international standard for representing dates and times. Common formats include:
PHP provides several functions for handling date strings and timestamps:
$timestamp = strtotime("2023-04-10 15:30:15");$date = date("Y-m-d");$date = strftime("%Y-%m-%dT%H:%M:%S");PHP allows you to define custom date formats, usually using the DateTime::format() method. Common format symbols include:
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.