Home php教程 php手册 Detailed explanation of the use of date and time function date() in PHP function_php basics

Detailed explanation of the use of date and time function date() in PHP function_php basics

May 16, 2016 am 09:00 AM
date datetime function

Date and time functions are a core component of PHP. No installation is required to use these functions. Let’s talk about the specific usage of date function in detail:

PHP Date() Function
The PHP Date() function formats a timestamp into a more readable date and time.

Syntax
date(format,timestamp)

format required. Specifies the format of the timestamp.
timestamp optional. Specify timestamp. The default is the current date and time.


PHP’s date and time function date()

Copy code The code is as follows:

$t=time();
echo date("Y-m-d H:i:s",$t);

The format of the first parameter respectively represents:
a - "am" or "pm"
A - "AM" or "PM"
d - day, two digits, If there are less than two digits, add zeros in front; such as: "01" to "31"
D - day of the week, three English letters; such as: "Fri"
F - month, full English name; such as: " January"
h - hour in 12-hour format; such as: "01" to "12"
H - hour in 24-hour format; such as: "00" to "23"
g - 12-hour format For hours in 24-hour format, do not add zeros for less than two digits; for example: "1" to 12"
G - For hours in 24-hour format, do not add zeros for less than two digits; for example: "0" to "23"
i - Minutes; such as: "00" to "59"
j - day, two digits, if there are less than two digits, do not add zero; such as: "1" to "31"
l - day of the week, in full English name; such as: "Friday"
m - month, two digits, if less than two digits, add zeros in front; such as: "01" to "12"
n - month, two digits, if less than two digits If two digits are used, zeros will not be added; such as: "1" to "12"
M - month, three English letters; such as: "Jan"
s - seconds; such as: "00" to "59"S - add an English ordinal number at the end of the word, two English letters; such as: "th", "nd"
t - specify the number of days in the month; such as: "28" to "31"
U - total seconds Number
w - numeric day of the week, such as: "0" (Sunday) to "6" (Saturday)
Y - year, four digits; such as: "1999"
y - year, Two digits; such as: "99"
z - the day of the year; such as: "0" to "365"
Other characters not listed above will be listed directly

1, year-month-day

Copy code The code is as follows:

echo date('Y-m-j');2007-02-6

echo date('y-n-j');
07-2-6


Capital Y means four digits of the year numbers, while lowercase y represents the two-digit number of the year;
lowercase m represents the number of the month (with a leading), while lowercase n represents the number of the month without the leading.

Copy code The code is as follows:

echo date('Y-M-j' );
2007-Feb-6

echo date('Y-m-d');
2007-02-06

The uppercase M represents the 3 abbreviation characters of the month, while the lowercase m represents the number of the month (with leading 0);
There is no uppercase J, only the lowercase j represents the date of the month, without the leading o; if the month is required Use a lowercase d when leading.

Copy code The code is as follows:

echo date('Y-M-j' );
2007-Feb-6

echo date('Y-F-jS');
2007-February-6th


Capital M represents the three abbreviated characters of the month, while capital F represents the full English character of the month. (No lowercase f)
Capital S represents the suffix of the date, such as "st", "nd", "rd" and "th", depending on the date number.

Summary:
The year can be represented by uppercase Y and lowercase y;
The month can be represented by uppercase F, uppercase M, lowercase m and lowercase n (two ways to represent characters and numbers respectively) );
You can use lowercase d and lowercase j to represent the day, and uppercase S represents the suffix of the date.


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.

Copy code The code is as follows:

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".

Copy code The code is as follows:

echo date('G:i:s') ;
14:02:26

Capital G represents the hour in the 24-hour system, but without a leader; use capital H to represent the hour in the 24-hour system with a leader

Summary:
The letter g represents the hour without a leader , 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

Copy code The code is as follows:

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;
Small l represents the full English version of the day of the week (Tuesday);
Use capital D to represent the day of the week The 3-character abbreviation of a few (Tue).

Copy code The code is as follows:

echo date('w');

Today’s week: 2

Copy code The code is as follows:

echo date('W');

This week is the 06th week of the year

The lowercase w represents the day of the week, expressed in numeric form
The uppercase W represents the number of weeks in the year

Copy code The code is as follows:

echo date('t');

This month is 28 days

Copy code The code is as follows:

echo date('z');

Today is the 36th day of this year

Lowercase t represents the number of days in the current month
Lowercase z represents the day of the year today

4, others

Copy code The code is as follows:

echo date('T');
UTC

Capital T indicates the server’s time zone setting

Copy code The code is as follows:

echo date('I');
0

Capital I means to determine whether the current daylight saving time is, if true, return 1, otherwise 0

Copy code The code is as follows:

echo date('U');
1170769424

The uppercase U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp of the Unix time epoch.

Copy code The code is as follows:

echo date('c');
2007-02-06T14:24:43 00:00

Lowercase c represents the ISO8601 date. The date format is YYYY-MM-DD. The letter T is used to separate the date and time. The time format is HH:MM:SS. The time zone is represented by the offset from Greenwich Mean Time (GMT). .

Copy code The code is as follows:

echo date('r');
Tue, 06 Feb 2007 14:25:52 0000

Lowercase r represents the RFC822 date.

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)

How to create and name a file/folder based on current timestamp How to create and name a file/folder based on current timestamp Apr 27, 2023 pm 11:07 PM

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

PHP Warning: date() expects parameter 2 to be long, string given solution PHP Warning: date() expects parameter 2 to be long, string given solution Jun 22, 2023 pm 08:03 PM

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

How to get the millisecond representation of a date using the getTime() method of the Date class How to get the millisecond representation of a date using the getTime() method of the Date class Jul 24, 2023 am 11:42 AM

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

Introduction to methods and usage of using Date and SimpleDateFormat classes to process time in Java Introduction to methods and usage of using Date and SimpleDateFormat classes to process time in Java Apr 21, 2023 pm 03:01 PM

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

How to use Stringbuild, Date and Calendar classes in Java How to use Stringbuild, Date and Calendar classes in Java May 22, 2023 pm 04:52 PM

Stringbuild class Since the object content of the String class cannot be changed, a new String object will be constructed every time it is spliced, which is time-consuming and wastes memory space. At this time, you need to solve this problem through the StringBuild class provided by Java. StringBuilder is also called a variable character sequence. , it is a string buffer similar to String, which can be regarded as a container. Many strings can be held in the container. Variable means that the content in the StringBuilder object is variable. The construction method publicStringBuilder(): creates an empty buffer publicStringBuilder(Stringsr

What are the options for calendar and date libraries in Python? What are the options for calendar and date libraries in Python? Oct 21, 2023 am 09:22 AM

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date

How to solve the problem of springboot configuring date field to return timestamp How to solve the problem of springboot configuring date field to return timestamp May 20, 2023 am 11:16 AM

I encountered a problem. After springboot was upgraded to 2.0, the date found from the database was received using Date and finally returned directly to the front end. It can be displayed normally in the yyyy-MM-ddHH:mm:ss format in Google Chrome. However, the date displayed in the IE browser is "garbled" because the springboot1.x version returns a timestamp in the Date field by default, and Google and IE will automatically convert the timestamp into yyyy-MM-ddHH:mm:ss ;After springboot2.0, spring will automatically convert the Date field into a UTC string (without configuration), so date needs to be converted into a timestamp or y

PHP formats a local time/date PHP formats a local time/date Mar 21, 2024 pm 02:30 PM

This article will explain in detail about formatting a local time/date in PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Formatting Local Time/Date Formatting local time and date is a common task in PHP and can be achieved through PHP's built-in functions and classes. Built-in functions PHP provides several built-in functions to format time and date: date(): used to format the current time and date and return the result according to the provided format string. strftime(): Similar to date(), but it uses the POSIX strftime() function to provide more advanced formatting options. format parameter date

See all articles