In PHP, the use of date and time functions is very important. Whether it is processing timestamps, calculating time differences, formatting output times, etc., PHP's date and time functions are very powerful and flexible. However, due to the numerous date and time functions in PHP, some developers may be confused as to which function to choose to complete a specific task. Therefore, this article will introduce some of the most commonly used and best date and time functions in PHP.
- time() Function The time() function returns the timestamp of the current time (number of seconds since January 1, 1970). If no argument is given, the current server time will be used. For example:
strtotime() function
- strtotime() function can convert any English text datetime description into a timestamp. For example, passing the string "1 day" to the strtotime() function will return tomorrow of the current date. Here are some examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | echo strtotime ( "now" );
echo strtotime ( "+1 day" );
echo strtotime ( "+1 week" );
echo strtotime ( "next Monday" );
echo strtotime ( "2021-12-25 12:00:00" );
|
Copy after login
date() function
- date() function can format the output date and time. It accepts a format string parameter to specify the format of the output results. Here are some commonly used formatting parameters:
1 2 3 4 5 | echo date ( "Y-m-d" );
echo date ( "Y-m-d H:i:s" );
|
Copy after login
mktime() function
- mktime() function can be used to create timestamps. It accepts multiple parameters including hour, minute, second, month, day, year, etc. to create a timestamp for a specific date time. The following is an example:
1 2 3 | $timestamp = mktime (0, 0, 0, 10, 1, 2021);
echo $timestamp ;
|
Copy after login
Calculation of time() and mktime()
- Using the time() function and mktime() function, you can easily calculate any two The time difference between dates. For example, the following example calculates the time difference between the current date and January 1, 2022:
1 2 3 4 5 6 | $today = time();
$future = mktime (0, 0, 0, 1, 1, 2022);
$diff = $future - $today ;
echo "还有 " . floor ( $diff / (60 * 60 * 24)) . " 天到 2022 年 1 月 1 日。" ;
|
Copy after login
Combined use of strtotime() and date()
- strtotime() function and the date() function can be used together to format the output of a string specifying a date and time. For example, the following example will output the current date and time in the "Year-Month-Day Hour:Minute:Second" format:
1 2 | echo date ( "Y-m-d H:i:s" , strtotime ( "now" ));
|
Copy after login
DateTime object
- DateTime is a user PHP built-in class for handling dates and times. It provides some convenient methods such as add(), sub(), diff(), etc. to facilitate processing of dates and times. Here is an example using the DateTime class:
1 2 3 4 | $date = new DateTime();
$date ->add( new DateInterval( 'P1D' ));
echo $date ->format( 'Y-m-d' );
|
Copy after login
Summary
In PHP, the use of date and time functions is very important. Whether you are processing timestamps, calculating time differences or formatting output times, you need to use date and time functions to complete it. Because each website needs to use different time formats and various customized time functions over time, it is very necessary to be familiar with the use of date and time functions. In this article, we introduce some of the most commonly used and best date and time functions in PHP for developers to learn from.
The above is the detailed content of Best date and time functions in PHP. For more information, please follow other related articles on the PHP Chinese website!