Date and Time in PHP

阅读更多
Displaying the current date and time with the date() function

The most used function is one that returns the current date and time, and allows you to format as you wish - the date function. And using it is very simple:
date("formatting_option").

There are a whole range of possible formatting options. You can add your own characters inside the format string too. Here's a list of all the formatting characters.
a 	"am" or "pm"
A 	"AM" or "PM"
B 	Swatch Internet time
d 	day of the month, 2 digits with leading zeros; i.e. "01" to "31"
D 	day of the week, textual, 3 letters; i.e. "Fri"
F 	month, textual, long; i.e. "January"
g 	hour, 12-hour format without leading zeros; i.e. "1" to "12"
G 	hour, 24-hour format without leading zeros; i.e. "0" to "23"
h 	hour, 12-hour format; i.e. "01" to "12"
H 	hour, 24-hour format; i.e. "00" to "23"
i 	minutes; i.e. "00" to "59"
I (capital i) 	"1" if Daylight Savings Time, "0" otherwise.
j 	day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') 	day of the week, textual, long; i.e. "Friday"
L 	boolean for whether it is a leap year; i.e. "0" or "1"
m 	month; i.e. "01" to "12"
M 	month, textual, 3 letters; i.e. "Jan"
n 	month without leading zeros; i.e. "1" to "12"
r 	RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP 4.0.4)
s 	seconds; i.e. "00" to "59"
S 	English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
t 	number of days in the given month; i.e. "28" to "31"
T 	Timezone setting of this machine; i.e. "MDT"
U 	seconds since the epoch
w 	day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
Y 	year, 4 digits; i.e. "1999"
y 	year, 2 digits; i.e. "99"
z 	day of the year; i.e. "0" to "365"
Z 	timezone offset in seconds (i.e. "-43200" to "43200"). The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.


Some examples:

//returns the day (0-31), month (3 letters) and year(4 digits)
print date("d-M-Y");


displays:
09-May-2002

The '-' characters are my formatting strings. I could have used anything else too:

print date("d^^^M^^^Y");


displays:

09^^^May^^^2002


print date("D dS M,Y h:i a");


displays:

Thu 09th May,2002 06:13 pm


Read through the list above - you'll find most possibilities have been thought of, and if all you're trying to do is format the date and/or time, you'll have no problems.

The date function is not only tied to displaying the local datetime on the server. You can also pass it a timestamp (often called a Unix timestamp, since it developed on Unix machines), which is the number of seconds since January 1, 1970 (starting with 1 second after midnight). By default the date function takes the current timestamp, but you can pass any timestamp you want, for example:

print date("l M dS, Y, H:i:s",5678)


displays:

Thursday Jan 01st, 1970, 03:34:38


Be careful though with trying to use one of the reserved format strings as your own format string. If you tried to do something like:

print date("The time is: H.i");


you'd get something like the unexpected result of:

SAST11e 314605e 4638: 23.46


Of the first word, 'The', the 'T' returns the time zone setting of the machine running PHP (SAST in my case), 'h' returns the hour, while at least the 'e' comes though correctly. But from there it's all downhill. In order to get the correct display, you'll need to escape the characters with a backslash, as follows:

print date("\T\he \\t\i\m\e \i\s: H.i");


which displays what we'd originally hoped for:

The time is: 23.54


Just for good measure we had to escape the 't' twice, since '\t' is the special character for a tab. Not the easiest to read at all! Later in this article, we look at the strftime() function, which makes formatting complex strings easier, and also helps with other languages.

There is also a related gmdate() function, which is the same as date(0 except that it returns the Greenwich Mean Time.

你可能感兴趣的:(PHP,Unix,F#,J#)