This article will introduce common date processing functions in PHP, including date formatting, timestamp conversion, date calculation and other operations.
In PHP, we can use the date() function to format the date into the desired string, the syntax is as follows:
date(format, timestamp);
Among them, the format parameter specifies the output format of the date, and the timestamp parameter is a UNIX timestamp. If the timestamp parameter is not specified, the current time is used by default.
The following are common formatting parameters:
Format parameters | Description |
---|---|
Y | Four-digit year |
y | Two-digit year |
m | Two-digit month (01-12) |
d | Two-digit day (01-31) |
H | Hour number in 24-hour format (00-23) |
i | two digits Number of minutes to count (00-59) |
s | Number of two-digit seconds (00-59) |
w | The numerical representation of the day of the week (0-6) |
L | Whether it is a leap year (1=yes, 0=no) |
For example, we can use the following code to format the current time into a string:
echo date('Y-m-d H:i:s');
The output is as follows:
2021-01-01 12:30:00
The timestamp is usually Unix time, that is, the number of seconds that have elapsed since 8:00 am on January 1, 1970 (UTC 8 time zone). If you need to convert a timestamp into a date, you can use the date() function or some other functions.
2.1 Use the date() function
To convert the timestamp into a date, you can use the date() function, the syntax is as follows:
date(format, timestamp);
Among them, the format parameter is the date Output format, timestamp parameter is UNIX timestamp.
For example, we can use the following code to convert a timestamp into a date string:
$timestamp = 1609459200; //2021-01-01 00:00:00 的时间戳 echo date('Y-m-d H:i:s', $timestamp);
The output is as follows:
2021-01-01 00:00:00
2.2 Use the strtotime() function
strtotime() function can convert a date string into a UNIX timestamp. Its syntax is as follows:
strtotime(timeString, now);
Among them, the timeString parameter is a date string, and the now parameter is an optional parameter, indicating the basis for calculating time. point, defaults to the current time.
For example, we can use the following code to convert a date string to a timestamp:
$dateString = '2021-01-01 00:00:00'; $timestamp = strtotime($dateString); echo $timestamp;
The output is as follows:
1609459200
In PHP, you can also add and subtract dates, such as calculating future or past dates, calculating the difference between dates, etc.
3.1 Use the date_modify() function
You can use the date_modify() function to add and subtract dates. The syntax is as follows:
date_modify(object, modify);
Among them, the object parameter represents the date object, The modify parameter indicates the modification to be made. You can use relative values (e.g. 1 day, -1 week) or absolute values (e.g. 2021-02-01).
For example, we can use the following code to calculate the date of the next month:
$dateString = '2021-01-01 00:00:00'; $datetime = new DateTime($dateString); $datetime->modify('+1 month'); echo $datetime->format('Y-m-d H:i:s');
The output result is as follows:
2021-02-01 00:00:00
3.2 Use the date_diff() function
You can use the date_diff() function to calculate the difference between two dates. The syntax is as follows:
date_diff(datetime1, datetime2, absolute);
Among them, the datetime1 and datetime2 parameters represent two date objects, and the absolute parameter represents whether to take the absolute value.
For example, we can use the following code to calculate the number of days between two dates:
$dateString1 = '2021-01-01 00:00:00'; $datetime1 = new DateTime($dateString1); $dateString2 = '2021-01-10 00:00:00'; $datetime2 = new DateTime($dateString2); $interval = date_diff($datetime1, $datetime2); echo $interval->days;
The output result is 9, indicating that the difference between the two dates is 9 days.
The above are the common date processing functions in PHP, including date formatting, timestamp conversion, date calculation and other operations. Mastering these functions will help us work with dates and times more easily.
The above is the detailed content of PHP common date processing functions. For more information, please follow other related articles on the PHP Chinese website!