Home > Article > Backend Development > Summary of usage of date and time function date() in PHP_PHP tutorial
date() is a commonly used date and time function. Let me summarize the various forms of usage of the date() function. Friends who need to learn can refer to it.
Format Date
The first parameter of the date() function specifies how to format the date/time. It uses letters to represent date and time formats. Here are some available
:
•d - day of the month (01-31)
•m - Current month as a number (01-12)
•Y - Current year (four digits)
You can find all the letters that can be used in the format parameter in our PHP Date reference manual.
You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formatting:
The code is as follows | Copy code | ||||
echo " ";
echo date("Y.m.d");
?> |
代码如下 | 复制代码 |
echo date('Y-m-j'); echo date('y-n-j'); |
2006/07/11
2006.07.11
代码如下 | 复制代码 |
echo date('Y-M-j'); echo date('Y-m-d'); |
1. Year-month-day
The code is as follows | Copy code | ||||
2007-02-6
07-2-6 |
The code is as follows | Copy code | ||||
echo date('Y-M-j'); echo date('Y-m-d'); 2007-02-06
|
The code is as follows | Copy code |
echo date('Y-M-j'); 2007-Feb-6 echo date('Y-F-jS'); 2007-February-6th |
The code is as follows | Copy code |
echo date('g:i:s a'); 5:56:57 am echo date('h:i:s A'); 05:56:57 AM |
A lowercase g indicates a 12-hour format without leading 0s, while a lowercase h indicates a 12-hour format with leading 0s.
When using the 12-hour clock, it is necessary to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".
The code is as follows | Copy code | ||||
14:02:26 |
Capital G represents the hour in 24-hour format, but without leading; use capital H to represent the hour in 24-hour format with leading
Summary:
Lowercase g and h represent the 12-hour format, while uppercase G and H represent the 24-hour format.
代码如下 | 复制代码 |
echo date('L'); |
代码如下 | 复制代码 |
echo date('l'); |
代码如下 | 复制代码 |
echo date('D'); |
Today is: Tue
Capital L indicates whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
代码如下 | 复制代码 |
echo date('w'); |
代码如下 | 复制代码 |
echo date('W'); |
This week is week 06 of the year
代码如下 | 复制代码 |
echo date('t'); |
This month is 28 days
代码如下 | 复制代码 |
echo date('z'); |
Today is the 36th day of the year
Lowercase t represents the number of days in the current month
Lowercase z indicates what day of the year today is
4, other
The code is as follows | Copy code | ||||
echo date('T');
|
Capital T indicates the server’s time locale
The code is as follows | Copy code | ||||
echo date('I');
|
Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0
The code is as follows | Copy code | ||||
echo date('U');
|
Capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.
The code is as follows | Copy code | ||||
echo date('c');
|
Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, use the letter T to separate the date and time, the time format is HH:MM:SS, and the time zone uses Greenway
Expressed by the deviation from GMT.
The code is as follows | Copy code | ||||
echo date('r');
|
Lowercase r indicates RFC822 date.
Add timestamp
The second parameter of the date() function specifies a timestamp. This parameter is optional. If you do not provide a timestamp, the current time will be used.
In our example, we will use the mktime() function to create a timestamp for tomorrow.
The mktime() function returns a Unix timestamp for a specified date.
Grammar
mktime(hour,minute,second,month,day,year,is_dst) If we need to get the timestamp of a certain day, we only need to set the
day parameter will do:
The code is as follows | Copy code | ||||
|
The output of the above code is similar to this:
Tomorrow is 2006/07/12
There are also some more advanced date and time functions to introduce to you
This category will introduce more functions to enrich our applications.
The code is as follows | Copy code | ||||
|
This function can be used to check and validate a date before it is used in calculations or saved in the database.
代码如下 | 复制代码 |
// returns false getdate($ts) |
The code is as follows | Copy code |
// returns false getdate($ts) |
With no arguments, this function returns the current date and time in a combined array. Each element in the array represents one of the date/time values
代码如下 | 复制代码 |
mktime($hour, $minute, $second, $month, $day, $year) |
Apply this function to obtain a series of discrete, easily separable date/time values.
The code is as follows | Copy code | ||||
// get date as associative array $arr = getdate(); echo "Date is " . $arr['mday'] . " " . $arr['weekday'] . " " . $arr['year']; echo "Time is " . $arr['hours'] . ":" . $arr['minutes']; ?> mktime($hour, $minute, $second, $month, $day, $year)
|
This function does the opposite of getdate(): it generates a UNIX time stamp from a series of date and time values (GMT time January 1, 1970 to
Number of seconds elapsed now). When no arguments are used, it generates a UNIX time stamp of the current time.
Use this function to obtain the UNIX time label of the immediate time. Such timestamps are commonly used in many databases and programming languages.
The code is as follows | Copy code | ||||
echo mktime(13,15,23,6,7,2006); ?> date($format, $ts) |
The code is as follows | Copy code |
// format current date // returns "13-Sep-2005 01:16 PM" echo date("d-M-Y h:i A", mktime()); ?> strtotime($str) |
This function converts a human-readable English date/time string into a UNIX time tag.
Apply this function to convert a non-standardized date/time string into a standard, compatible UNIX time tag.
The code is as follows | Copy code | ||||
echo date("d-M-y", strtotime("today")); // returns 14-Sep-05 echo date("d-M-y", strtotime("tomorrow")); // returns 16-Sep-05 echo date("d-M-y", strtotime("today +3 days")); ?> strftime($format,$ts)
|
代码如下 | 复制代码 |
// set locale to France (on Windows) // format month/day names echo strftime("Month: %B "); |
Apply this function to create a date string compatible with the current environment.
The code is as follows | Copy code |
// set locale to France (on Windows) // format month/day names echo strftime("Month: %B "); |
代码如下 | 复制代码 |
// run some code // get ending value // calculate time taken for code execution |
microtime()
As defined by the previous setlocale() function, this function formats the UNIX time stamp into a date string suitable for the current environment.
Apply this function to create a date string compatible with the current environment.
The code is as follows | Copy code | ||||||||||||||||||||||||
// get starting value $start = microtime(); // run some code for ($x=0; $x<1000; $x++) { $null = $x * $x; }
// get ending value // calculate time taken for code execution
date_default_timezone_set($tz), date_default_timezone_get() All date/time function calls after this function set and restore the default time zone. Note: This function is only valid in PHP 5.1+. This function is a convenient shortcut for setting the time zone for future time operations.
Statement: 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 Previous article:Function to calculate string similarity in PHP_PHP tutorialNext article:Function to calculate string similarity in PHP_PHP tutorial Related articlesSee more |