Home > Backend Development > PHP Tutorial > PHP gets the current time (year, month, day, hour, minute, second)

PHP gets the current time (year, month, day, hour, minute, second)

巴扎黑
Release: 2016-11-22 16:18:18
Original
2728 people have browsed it

PHP gets the current time (year, month, day, hour, minute, second)
1echo date( "h:i ");
date
(PHP 3, PHP 4)
date — Format a local time/date
Description
string date (string format [, int timestamp])
Returns a string generated by converting the integer timestamp according to the given format string. If no timestamp is given, the local current time is used.
Note: The typical range of valid timestamps is December 13, 1901 20:45:54 GMT to January 19, 2038 03:14:07 GMT. (This range conforms to the minimum and maximum values ​​of 32-bit signed integers). On Windows systems this range is limited to January 1, 1970 to January 19, 2038.
To convert the time expressed in a string into a timestamp, strtotime() should be used. In addition, some databases have functions to convert their time format into timestamps (such as MySQL's UNIX_TIMESTAMP function).
The format string can recognize the following characters:
a – “am” or “pm”
A – “AM” or “PM”
B – Swatch Internet Time ([Translator’s Note] See http://swatch.com/ alu_beat/fs_itime.html)
d – The day of the month, 2 digits with leading zeros, such as “01” to “31”
D – The day of the week, text representation, 3 letters, such as “Fri”
F – Month, complete text format, e.g. “January”
g – Hour, 12-hour format, no leading zeros, e.g. “1” to “12”
G – Hour, 24-hour format, no leading zeroes , such as "0" to "23"
h - hour, 12-hour format, such as "01" to "12"
H - hour, 24-hour format, such as "00" to "23"
i - minute, such as " 00 ” to “59 ”
I (the uppercase letter of “i”) – “1” if it is daylight saving time, otherwise “0”
j – The day of the month, without leading zeros, such as “1 ” to “31”
l (lower case letter of “L”) – Day of the week, complete text format, such as “Friday”
L – Boolean value indicating whether it is a leap year, such as “0” or “1”
m – month, e.g. “01” to “12”
M – month, text representation, 3 letters, e.g. “Jan”
n – ​​month, no leading zero, e.g. “1” to “12”
O – with The number of hours behind Greenwich Mean Time, such as "+0200"
r - a date in RFC 822 format, such as "Thu, 21 Dec 2000 16:01:07 +0200 " (new in PHP 4.0.4)
s - seconds Number, such as “00” to “59”
S – English suffix after the number of days of the month, 2 characters, such as “st”, “nd”, “rd” or “th”
t – The number of days in a given month The number of days, such as "28" to "31"
T – The time zone where the machine is located, such as "EST" or "MDT" ([Translator's Note] In full text format under Windows, such as "Eastern Standard Time", Chinese The version will display "China Standard Time")
U - The number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT)
w - The day of the week, represented by a number, such as "0" ( Sunday) to "6" (Saturday)
W – The week number in the year in ISO-8601 format, each week starts on Monday (new in PHP 4.1.0)
Y – The year, 4 digits, such as "1999 ”
y – annual fee, 2 digits, such as “99”
z – day of the year, such as “0” to “365”
Z – seconds of time difference offset (such as “-43200” to "43200"). Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive.
Unrecognized characters in the format string will be displayed as they are. The "Z" format always returns "0" when using gmdate().
Example 1. date() Example
12echo date ( "l dS of F Y h:i:s A ");echo "July 1, 2000 is on a " . date ( "l ", mktime(0,0,0 ,7,1,2000));
Add a backslash before the characters in the format string to escape them to prevent them from being interpreted according to the above table. If the character after the backslash is itself a special sequence, the backslash must be escaped. Example 2. Escape characters in date()
echo date( “l \the jS “); // The display is similar to: Saturday the 8th
You can use date() and mktime() together to get future or past dates . Example 3. date() and mktime() Example
123$tomorrow = mktime (0,0,0,date( "m ") ,date( "d ")+1,date( "Y "));$lastmonth = mktime (0,0,0,date( "m ")-1,date( "d "), date( "Y "));$nextyear = mktime (0,0,0,date( "m ") , date( "d "), date( "Y ")+1);
Note: Due to daylight saving time, this method is more reliable than simply adding or subtracting seconds of a day or month to the timestamp.
Some examples of using date() to format dates. Be careful to escape all other characters, as characters with special meanings currently produce undesired results, and the remaining characters may be used in future versions of PHP. When escaping, be careful to use single quotes to avoid characters like n becoming newlines. Example 4. date() Formatting
12345678910/* Today is March 10th, 2001, 5:16:18 pm */$today = date( "F j, Y, g:i a "); // March 10, 2001, 5:16 pm$today = date( "m.d.y "); // 03.10.01$today = date( "j, n, Y "); // 10, 3, 2001$today = date( "Ymd "); // 20010310$today = date( 'h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01$today = date( 'it is the jS day . '); // It is the 10th day.$today = date( "D M j G:i:s T Y "); // Sat Mar 10 15:16:08 MST 2001$today = date( 'H:m :s m is month '); // 17:03:17 m is month$today = date( "H:i:s "); // 17:16:17
To format dates in other languages, you should use setlocale () and strftime() functions.
See getlastmod(), gmdate(), mktime(), strftime() and time().

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template