In the process of developing and understanding PHP, timestamp (timestamp) and date (date) are concepts that are often used. When processing time data, you need to convert timestamps to dates, or dates to timestamps.
PHP provides a variety of functions to easily convert between timestamps and dates. This article will introduce in detail the usage of the date() function in PHP and its conversion relationship with timestamps.
A timestamp refers to a way of representing time, that is, from a fixed point in time (usually 00:00 on January 1, 1970) 00:00 UTC), counting the number of seconds that have elapsed since then. In PHP, by calling the time() function, you can get the timestamp of the current time, as shown below:
$timestamp = time();
date() is The PHP function used to format dates can output the date and time in the specified format. The syntax is:
date($format, $timestamp)
Among them, $format is the date output format, $timestamp is an optional parameter, representing the timestamp that needs to be formatted. If the timestamp is not passed in, the current timestamp will be used by default. The following are some commonly used format parameters:
Parameter | Description |
---|---|
Y | Four-digit year |
m | Numeric month (with leading zero) |
d | Day of the month (with leading zeros) |
H | Hour (24 hour format, with leading zeros) |
i | Minutes (with leading zeros) |
s | Seconds (with leading zeros) Zero) |
w | The day of the week (0 represents Sunday, 1 represents Monday, and so on) |
a | AM or PM (lower case) |
A | AM or PM (upper case) |
For example, to format timestamp 1609459584 into a string in the form of "2021-01-01 12:12:12", you can use the following code:
echo date('Y-m-d H:i:s', 1609459584);
The output result is:
2021-01-01 12:12:12
If we already have a timestamp, we can use the date() function to convert it to a date string in the specified format, as shown below :
$timestamp = 1609459584; $datetime = date('Y-m-d H:i:s', $timestamp); echo $datetime;
If we have a date string and need to convert it to a timestamp, we can use the strtotime() function. This function converts a string time to a UNIX timestamp.
$datetime = '2021-01-01 12:12:12'; $timestamp = strtotime($datetime); echo $timestamp;
The output result is:
1609459584
It should be noted that the input time of the strtotime() function must be in English date format. For example, the time given in the above example is "2021- 01-01 12:12:12", if the time format is incorrect, the function will return false.
The date() and strtotime() introduced above use the local time of the server by default. But sometimes you need to use a specified time zone when processing time. You can use the date_default_timezone_set() function to set the time zone.
date_default_timezone_set('Asia/Shanghai'); echo date('Y-m-d H:i:s');
The time format parameters remain unchanged, and the output date and time will be automatically converted to East Eighth District time:
2022-03-15 15:33:57
In PHP, Conversion of timestamp and date is a very common operation. These conversions can be easily accomplished through the date() function and strtotime() function introduced in this article.
The usage scenarios are also very extensive. For example, when publishing an article on a website, we need to convert timestamps into dates, or convert human-readable dates into timestamps for comparison. No matter which scenario you are in, it is very important to master these functions.
The above is the detailed content of How to use date() to convert timestamp in php. For more information, please follow other related articles on the PHP Chinese website!