PHP date function

 The PHP date function can get the server's date and time and format it (Format a local time/date). The PHP date function is quite powerful, except for formatting day, week, month, year... , You can also display AM in the morning, PM in the afternoon, 12-hour format, 24-hour format, etc. As long as you learn how to control the Format parameter, you can display the time you want to display as you want, and the PHP date function is a built-in function It can be used directly without additional installation package, which is the basic introductory skill for learning PHP programming.


Basic syntax of PHP date function

string date ( string $format [, int $timestamp ] );


It seems how to use a whole bunch of grammars? It’s actually very simple. The focus is on the date and the two parameters in parentheses. A few examples will be introduced later. Let’s take a look at the meaning of these two parameters. The first parameter, $format, is to get The host time should be formatted in which mode, such as day, month, year, hour, minute, second... and so on, it must be filled in according to the built-in format table of the date function, the second parameter $ timestamp is the time Stamp, also often called Unix timestamp, is the time from January 1, 1970 (00:00:00 GMT) to the present, in seconds, and is not a required item.

PHP date function commonly used $format parameter
$format parameters related to the day
dFormatted as a date counted by month, written as date("d");, for example, the fifth day of the month is 05.
DFormat as abbreviation of the day of the week, written as date("D");, for example, Wednesday is Wed.
j
Formatted as a digital day counted in days, written as: date("j"); without zeros, for example, the fifth day of the month is 5.
lFormatted as a complete word of the day of the week, the parameter is a lowercase L, written: date("l");, such as Wednesday.
$format parameters related to the week
INUse numbers to indicate the week of the year, for example, the 20th week, output 20 directly, written as date("W"). PHP 4.1.0 is new.
$format parameter related to month
FOutput the English word of the month of the current month, written as date ("F"), for example, June is output for June and October is output for October.
m
The month of the current month is represented by a number, written as date ("m"), with a zero in front of the single digit, for example, June is 06.
M
Express the current month in English abbreviations of the month, written date ("M"), for example, June is Jun.
n
The month of the current month is expressed as a number, written as date("n"), and zeros are not added before the single digit. For example, June is 6.
t
Directly output the number of days in the current month, written as date("t"). For example, if June is a small month, it will output 30, which means there are 30 days in total.
$format parameters related to the year
andIt is expressed as a two-digit abbreviated year, written as date ("y"), for example, 2013 is 13.
AND
It is expressed as a full four-digit year, written as date("Y"), for example, 2013 is output as 2013.
Time display format
a
Lower case, the judgment of am in the morning or pm in the afternoon, with 12 noon as the dividing line, written as date("a").
A
Capitalized, the judgment of AM in the morning or PM in the afternoon, with 12 noon as the dividing line, written as date ("A").
g
Hour, display the time format of the 12-hour system, written as date("g"), with no zeros in front of the single digit, for example, 13 o'clock is 1.
G
Hour, display the time format of the 24-hour system, written as date ("G"), without zeros before the single digit, for example, 13 o'clock is 13.
h
Hour, display the 12-hour time format, written as date ("h"), with zeros in front of the single digit, for example, 01 at 13 o'clock.
H
Hour, display the time format of the 24-hour system, written as date ("H"), with zeros in front of the single digit, for example, 13 o'clock is 13.
i
Minutes, displaying 00 to 59 minutes, written as date ("i"), with zeros in front of the single digit, for example, 6 minutes will display 06.
s
Seconds, display 00~59 seconds, written as date("s"), with zeros in front of the single digit, for example, 06 is displayed for 6 seconds.

PHP date function example

With the above PHP date function $format parameter list, we can create many time format examples.
<?php
echo date("Ymd H:i:s"); // Commonly used complete notation, respectively year, month, day, hour, minute, second, the output is similar to 2013-06-05 05:12: 50.
echo date("mdy"); // The time is divided into points, which are month, day, and year, and the output is similar to 06.05.13.
echo date("mdY"); // The time is distinguished by dots, which are month, day, and year. The difference lies in the display of the year. The output is similar to 06.05.2013.
echo date('Y year m month d day'); // Further bring Chinese into the display result, the output is similar to: June 05, 2013.
echo date('Y year n month j day'); // Eliminate the zeros of the month and day, the output is similar: June 5, 2013.
?>
The PHP date function is so simple and easy to use, but it is more important to note that there is a part of the output of Traditional Chinese in the example. The encoding of the PHP code file itself must support Traditional Chinese to ensure no error, otherwise it is likely to output garbled characters. It is recommended to use UTF-8 as the main encoding principle.

Editing record: Thanks to netizens for providing the "week" parameter should be capitalized "W", the content has been revised. .. 2014/6/3

Post a Comment

0 Comments