


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 |
Capital Y represents the four-digit year, while lowercase y represents the two-digit year;
Lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.
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 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
![]() Hot AI Tools![]() Undresser.AI UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() AI Hentai GeneratorGenerate AI Hentai for free. ![]() Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics
CakePHP Tutorial
![]() ![]() ![]() PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati ![]() Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c ![]() If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op ![]() This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an ![]() JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably, ![]() A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total ![]() Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead. ![]() What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency. ![]() |