Home Backend Development PHP Tutorial PHP Date() function detailed parameters

PHP Date() function detailed parameters

Nov 08, 2016 am 11:31 AM

Add
date_default_timezone_set(PRC) to the front page of the page; /*Adjust the time to Beijing time, php5 defaults to Greenwich Mean Time*/
date ()
a: "am" or "pm"
A : "AM" or "PM"
d: Day, two digits, zero if missing; from "01" to "31"
D: Day of the week, 3 English letters, such as: "Fri"
F: Month, full English name, such as: "January"
h: Hours in 12-hour format, from "01" to "12"
H: Hours in 24-hour format, from "00" to "23"
g: Hours in the 12-hour format do not add zeros; from "1" to "12"
G: Hours in the 24-hour format do not add zeros; from "0" to "23"
j: Days, any gaps will not be padded with zeros; From "1" to "31"
l: Day of the week, full English name, such as: "Friday"
m: Month, two digits, from "01" to "12"
n: Month, two digits, not Fill in zeros; from "1" to "12"
M: Month, 3 English letters; such as: "Jan"
s: Seconds; from "00" to "59"
S: Add an English ordinal at the end of the word, two English letters, such as: "21th"
t: The number of days in the specified month, from "28" to "31"
U: The total number of seconds
w: The numeric day of the week, from "0 (Sunday)" to "6 ( Saturday)"
Y: Year, four digits
y: Year, two digits
z: Day of the year; from "1" to "366"
============ ================================================== =====
1, year-month-day
indicates the year with uppercase Y and lowercase y;
indicates the month with uppercase F, uppercase M, lowercase m and lowercase n (representing two types of characters and numbers respectively) Method);
can use lowercase d and lowercase j to represent the day, and uppercase S represents the suffix of the date.
echo date('Y-m-j');
2007-02-6
echo date('y-n-j');
07-2-6
Uppercase Y represents the four-digit year, while lowercase y represents the two digits of the year digits;
lowercase m represents the number of the month (with leading), while lowercase n represents the number of the month without the leading.
echo date('Y-M-j');
2007-Feb-6
echo date('Y-m-d');
2007-02-06
Capital M represents the 3 abbreviated characters of the month, while lowercase m represents The number of the month (with leading 0);
There is no uppercase J, only lowercase j represents the date of the month, without the leading o; if the month needs to be preceded, use lowercase d.
echo date('Y-M-j');
2007-Feb-6
echo date('Y-F-jS');
2007-February-6th
Capital M represents the 3 abbreviation characters of the month, while capital F represents the month written in full English. (No lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.
2, hours: minutes: seconds
By default, the time displayed by PHP interpretation is "Greenwich Mean Time", which is 8 hours different from our local time.
echo date('g:i:s a');
5:56:57 am
echo date('h:i:s A');
05:56:57 AM
lowercase g represents the 12-hour clock, There is no leading 0, and the lowercase h represents the 12-hour clock with leading 0.
When using the 12-hour clock, you need to indicate morning and afternoon. Lowercase a represents lowercase "am" and "pm", and uppercase A represents uppercase "AM" and "PM".
echo date('G:i:s');
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:
The letter g represents the hour without a leading, and the letter h represents the hour with a leading;
Lowercase g and h represent the 12-hour format, and capital G and H represent the 24-hour format.
3, leap year, week, day
echo date('L');
Whether this year is a leap year: 0
echo date('l');
Today is: Tuesday
echo date('D');
Today is: Tue
Capital L means to determine whether this year is a leap year, Boolean value, returns 1 if true, otherwise 0;
Lowcase l means the full English version of the day of the week (Tuesday);
Use capital D to represent the 3-character day of the week Abbreviation (Tue).
echo date('w');
Today's week: 2
echo date('W');
This week is the 06th week of the year
Lowercase w represents the day of the week, in numerical form
Capital W represents the year The number of the week in
echo date('t');
This month has 28 days
echo date('z');
Today is the 36th day of this year
Lowercase t means how many days in the current month
Lowercase z means today It is the day of the year
4, others
echo date('T');
UTC
uppercase T indicates the time zone setting of the server
echo date('I');
0
Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0
echo date('U');
1170769424
Capital U represents the total number of seconds from January 1, 1970 to the present, which is Unix time The UNIX timestamp of the epoch.
echo date('c');
2007-02-06T14:24:43+00:00
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 For HH:MM:SS, the time zone is expressed as an offset from Greenwich Mean Time (GMT).
echo date('r');
Tue, 06 Feb 2007 14:25:52 +0000
lowercase r represents the RFC822 date.
5. Format time
echo $row["t_time"]; will output 2008-2-29 12:08:00
echo date("Y-m-d",strtotime($row["t_time"])); will output 2008-2-29
Note, since the time obtained by $row["t_time"] is already a string, you need to use strtotime (string to timestamp) to convert it, otherwise the error of 1970-01-01 will be output

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 Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

What is the difference between include, require, include_once, require_once? What is the difference between include, require, include_once, require_once? Apr 05, 2025 am 12:07 AM

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles