Home > Backend Development > PHP Tutorial > PHP time and date related functions

PHP time and date related functions

王林
Release: 2023-05-23 08:28:01
Original
1031 people have browsed it

PHP, as a popular programming language, provides a rich built-in function library for time and date related operations. In daily development, we often need to use these functions, such as obtaining the current time, formatting dates and other operations. In this article, we will introduce some commonly used time and date-related functions, as well as their usage and examples.

  1. Timestamp function

The timestamp refers to the number of seconds that have elapsed since January 1, 1970 00:00:00 UTC (Coordinated Universal Time), It is the most basic time and date representation in PHP. PHP provides many functions related to timestamps, such as time(), date(), strtotime(), etc.

The time() function is used to obtain the timestamp of the current time, and its return value is an integer value.

Example:

$current_time = time();
echo $current_time;
Copy after login

date() function is used to format the date and needs to pass in two parameters. The first parameter is the format of the date, and the second parameter is the timestamp (can selected, the default is the current time).

Example:

$current_date = date('Y-m-d H:i:s', time());
echo $current_date;
Copy after login

The strtotime() function is used to parse a text date into a Unix timestamp.

Example:

$str_time = '2019-11-11 11:11:11';
$unix_time = strtotime($str_time);
echo $unix_time;
Copy after login
  1. Date formatting function

In actual development, we often need to format the date into a specified format and display it . PHP provides the date() function for formatting dates. Two parameters need to be passed in. The first parameter is the format of the date, and the second parameter is the timestamp (optional, defaulting to the current time).

Example:

$unix_time = time();
$date_format = 'Y年m月d日 H时i分s秒';
$current_date = date($date_format, $unix_time);
echo $current_date; 
Copy after login

In the above example, $date_format represents the date format that needs to be formatted. For example, Y represents the complete year, m represents the month (01-12), and d represents the month. The day (01-31), H represents the hour (00-23), i represents the minute (00-59), and s represents the second (00-59).

When formatting dates, if you need to insert fixed characters as delimiters, you can use backslash escape, such as Y-m-d H:i:s means the date format is yyyy-mm-dd hh:mm: ss.

  1. Other commonly used date and time functions

In addition to the functions introduced above, PHP also provides some other commonly used date and time functions, including:

(1) The mktime() function is used to obtain the timestamp of the specified time.

Example:

$unix_time = mktime(12, 0, 0, 12, 31, 2020);
echo $unix_time;
Copy after login

In the above example, the mktime() function accepts 6 parameters, representing hours, minutes, seconds, months, dates, and years respectively, and its return value is the Unix value of the specified time Timestamp.

(2) The date_diff() function is used to calculate the difference between two dates.

Example:

$datetime1 = new DateTime('2021-01-01');
$datetime2 = new DateTime('2021-03-15');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a 天');
Copy after login

In the above example, $interval->format('%a days') means to output the calculated number of days in a formatted manner.

(3) The date_add() function is used to add time to the specified date.

Example:

$date = new DateTime('2021-03-15');
$date->add(new DateInterval('P5D'));
echo $date->format('Y-m-d');
Copy after login

In the above example, 'P5D' means adding 5 days. Other time-related parameters include: Y (year), M (month), D (day), W (week), H (hour), I (minute), S (second).

Conclusion

This article introduces commonly used time and date-related processing functions in PHP, including timestamp functions, date formatting functions, date-time difference functions, date-time addition and subtraction functions, etc. In actual development, we can select corresponding functions to operate according to specific needs to improve development efficiency. At the same time, you need to pay attention to the need to use the correct parameters when formatting dates to avoid unnecessary problems.

The above is the detailed content of PHP time and date related functions. For more information, please follow other related articles on the PHP Chinese website!

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