Home > Article > Backend Development > Comprehensive analysis of date and time functions in PHP applications
PHP is a widely used server-side scripting language and an iconic development language of Open Source. As a huge language, PHP not only contains rich variables and data types, but also provides a variety of built-in functions so that developers can quickly solve problems. Among them, date and time functions are a very commonly used type of function.
In PHP, date and time are expressed in the form of strings, usually using year-month-day (YYYY-MM-DD) and hour: minute: second (HH:MM:SS) format. The date and time functions provided by PHP are very comprehensive, including getting the current date and time, formatting date and time, converting timestamps, etc. Below, we introduce the usage of these functions in detail.
1. Get the current date and time
The date() function is one of the most basic date and time functions in PHP One, used to get the current date and time. The usage of this function is very simple. You only need to enter one parameter and the current date and time information can be output according to the format specified by the parameter.
For example, to get the full format of the current date and time, you can use the following code:
<?php echo date("Y-m-d H:i:s"); ?>
The output result is the full format of the current date and time, for example:
2022-05-12 11:30:35
In addition to the complete format, you can also set different parameters to obtain dates and times in different formats according to your own needs.
The time() function is a function in PHP to obtain the current timestamp (the number of seconds since January 1, 1970). This function does not require any parameters, just call it directly.
For example, to get the current timestamp, you can use the following code:
<?php echo time(); ?>
The output result is the current timestamp, for example:
1652367060
2. Format date and Time
The strtotime() function is a function in PHP used to convert date and time strings into timestamps. This function is very flexible in usage and can accept date and time strings in multiple formats and convert them into corresponding timestamps.
For example, to convert the date string "2022-05-12" into a timestamp, you can use the following code:
<?php $timestamp = strtotime("2022-05-12"); echo $timestamp; ?>
The output result is the timestamp corresponding to the date, for example:
1652313600
date_create() and date_format() functions can be used together to convert date strings (including time) to Date object and format it.
For example, to convert the date string "2022-05-12 11:30:35" into the format of "Y year m month d day H hour i minute s second", you can use the following code:
<?php $date = date_create("2022-05-12 11:30:35"); echo date_format($date, "Y年m月d日 H时i分s秒"); ?>
The output result is a formatted date string, for example:
2022年05月12日 11时30分35秒
3. Time calculation
strtotime() function can not only convert date and time strings into timestamps, but also perform time calculations. This function accepts two parameters: the first parameter is the time string to be calculated, and the second parameter is the amount of time to be added or subtracted.
For example, to add 3 days to the date string "2022-05-12", you can use the following code:
<?php $timestamp = strtotime("+3 days", strtotime("2022-05-12")); echo date("Y-m-d", $timestamp); ?>
The output result is "2022-05-15".
The date_add() and date_diff() functions are functions in PHP used to add and subtract dates and calculate time differences. Among them, date_add() is used to add and subtract dates, and date_diff() is used to calculate the time difference between two dates.
For example, to add 3 days to the date string "2022-05-12", you can use the following code:
<?php $date = date_create("2022-05-12"); date_add($date, date_interval_create_from_date_string("3 days")); echo date_format($date, "Y-m-d"); ?>
The output result is "2022-05-15".
4. Other functions
mktime() function is used in PHP to obtain the timestamp corresponding to the specified date and time. function. The parameters of this function are hours, minutes, seconds, months, days and years, which can be set as needed.
For example, to get the timestamp corresponding to "2022-05-12 00:00:00", you can use the following code:
<?php $timestamp = mktime(0, 0, 0, 05, 12, 2022); echo $timestamp; ?>
The output result is "1652198400".
gettimeofday() function is a function in PHP used to get the current time (including microseconds). This function returns an array containing the current time information, including seconds and microseconds.
For example, to get the seconds and microseconds of the current time, you can use the following code:
<?php $time = gettimeofday(); echo $time["sec"] . "." . $time["usec"]; ?>
The output result is the seconds and microseconds of the current time, for example:
1652367060.048500
Summary
Date and time functions are one of the most commonly used functions in PHP. Mastering their usage can help us process date and time information quickly and accurately. Although the date and time functions introduced above are only a small part of PHP, they are sufficient to meet common date and time needs. I hope that through this article, you can have a certain understanding of the date and time functions in PHP.
The above is the detailed content of Comprehensive analysis of date and time functions in PHP applications. For more information, please follow other related articles on the PHP Chinese website!