Home > Backend Development > PHP Tutorial > PHP common date processing functions

PHP common date processing functions

WBOY
Release: 2023-06-23 08:38:01
Original
1886 people have browsed it

This article will introduce common date processing functions in PHP, including date formatting, timestamp conversion, date calculation and other operations.

  1. Date formatting

In PHP, we can use the date() function to format the date into the desired string, the syntax is as follows:

date(format, timestamp);
Copy after login
Copy after login

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 parametersDescription
YFour-digit year
yTwo-digit year
mTwo-digit month (01-12)
dTwo-digit day (01-31)
HHour number in 24-hour format (00-23)
itwo digits Number of minutes to count (00-59)
s Number of two-digit seconds (00-59)
wThe numerical representation of the day of the week (0-6)
LWhether 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');
Copy after login

The output is as follows:

2021-01-01 12:30:00
Copy after login
  1. Timestamp conversion

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);
Copy after login
Copy after login

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);
Copy after login

The output is as follows:

2021-01-01 00:00:00
Copy after login

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);
Copy after login

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;
Copy after login

The output is as follows:

1609459200
Copy after login
  1. Date calculation

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);
Copy after login

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');
Copy after login

The output result is as follows:

2021-02-01 00:00:00
Copy after login

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);
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template