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.
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;
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;
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;
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;
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.
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;
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 天');
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');
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!