PHP/时间范例大全
2010-12-15 15:25:11
由于对时间函数很有恐惧感,决定好好写个demo
PHP时间函数范例:
代码:
代码简要说明:主要有两个函数:showTime($timeFormat)和desc($descTime) ;
前者根据传递的格式显示时间,后者表示描述;
<html> <head> <title> PHP-Time Format </title> </head> <h2>PHP 时间和日期函数范例</h2> <h3>当前时间:<?php echo date('c');?></h3> <body> <table border="1"> <tr><th>格式</th><th>时间</th><th>描述</th></tr> <?php /*rorger,2010*/ //chinese english, so you can read these more familiarly function desc($descTime) { echo "<td>"; echo "$descTime"; echo "</td>"; echo "</tr>"; } function showTime($format) { echo "<tr>"; echo "<td>"; echo "$format"; echo "</td>"; echo "<td>"; echo date($format); echo "</td>"; } //We will talk about each about D M and Y and the we talk about HH:MM:SS at the end. //Day. echo "<th colspan='3'>Day</th>"; showTime('D M Y'); desc('//D--A textual representation of a day, three letters'); showTime('d M Y'); desc('//d--Day of the month, 2 digits with leading zeros'); showTime('j M Y'); desc('//j--Day of the month without leading zeros'); showTime('l M Y'); desc('//l--A full textual representation of the day of the week'); showTime('N M Y'); desc('//N --ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)'); showTime('dS M Y');desc('//S-- english ordinal suffix for the day of the month, 2 characters'); showTime('w M Y'); desc('// w-- the day of week.'); showTime('w z Y'); desc('// z --The day of the year.'); echo "<th colspan='3'>Week</th>"; //Week. showTime('W') ; desc('//W--ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)'); //Month echo "<th colspan='3'>Month</th>"; //focus your attention on the field represents Month showTime('l F Y');desc('// F -- A full textual representation of a month, such as January or March'); showTime('d m Y');desc('//m --Numeric representation of a month, with leading zeros'); showTime('D M Y');desc('//M --A short textual representation of a month, three letters'); showTime('dS n Y');desc('//n --Numeric representation of a month, without leading zeros'); showTime('t /d/a/y/s /i/n /m/o/n/t/h F');desc('// t--Number of days in the given month'); //Year echo "<th colspan='3'>Year</th>"; showTime('/L/e/a/p /Y/e/a/r: L');desc("//L --Whether it's a leap year"); showTime('d m o'); desc('//o--ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)'); showTime('d m Y'); desc('//Y--A full numeric representation of a year, 4 digits'); showTime('d m y'); desc('//y--A two digit representation of a year'); //Time echo "<th colspan='3'>Time</th>"; showTime('D M Y a'); desc('//a --Lowercase Ante meridiem and Post meridiem'); showTime('D M Y A'); desc('//A --Uppercase Ante meridiem and Post meridiem'); showTime('D M Y B'); desc('//B --Swatch Internet time'); showTime('D M Y g'); desc('//g --12-hour format of an hour without leading zeros'); showTime('D M Y G'); desc('//G --24-hour format of an hour without leading zeros'); showTime('D M Y h'); desc('//h --12-hour format of an hour with leading zeros'); showTime('D M Y H'); desc('//H --24-hour format of an hour with leading zeros'); showTime('D M Y i'); desc('//i --Minutes with leading zeros'); showTime('D M Y s'); desc('//s --Seconds, with leading zeros'); showTime('D M Y u'); desc('//u --Microseconds (added in PHP 5.2.2)'); showTime('D M Y g:i:s'); desc("show the time"); //Timezone echo "<th colspan='3'>Timezone</th>"; showTime('e');desc('//e --Timezone identifier (added in PHP 5.1.0)'); showTime('/i/n /d/a/y/l/i/g/h/t I');desc('//I (capital i) --Whether or not the date is in daylight saving time '); showTime('O'); desc('//O -- Difference to Greenwich time (GMT) in hours'); showTime('P'); desc('//P -- Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)'); showTime('T'); desc('//T --Timezone abbreviation'); showTime('Z'); desc('//Z --Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.'); //Full Date/Time echo "<th colspan='3'>Full Date/Time</th>"; showTime('c'); desc('//c --ISO 8601 date (added in PHP 5)'); showTime('r'); desc('//r --? RFC 2822 formatted date'); showTime('U') ; desc('//Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)'); ?> </table> </body> </html>
格式 | 时间 | 描述 |
---|---|---|
Day | ||
D M Y | Wed Dec 2010 | //D--A textual representation of a day, three letters |
d M Y | 15 Dec 2010 | //d--Day of the month, 2 digits with leading zeros |
j M Y | 15 Dec 2010 | //j--Day of the month without leading zeros |
l M Y | Wednesday Dec 2010 | //l--A full textual representation of the day of the week |
N M Y | 3 Dec 2010 | //N --ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) |
dS M Y | 15th Dec 2010 | //S-- english ordinal suffix for the day of the month, 2 characters |
w M Y | 3 Dec 2010 | // w-- the day of week. |
w z Y | 3 348 2010 | // z --The day of the year. |
Week | ||
W | 50 | //W--ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) |
Month | ||
l F Y | Wednesday December 2010 | // F -- A full textual representation of a month, such as January or March |
d m Y | 15 12 2010 | //m --Numeric representation of a month, with leading zeros |
D M Y | Wed Dec 2010 | //M --A short textual representation of a month, three letters |
dS n Y | 15th 12 2010 | //n --Numeric representation of a month, without leading zeros |
t /d/a/y/s /i/n /m/o/n/t/h F | 31 days in month December | // t--Number of days in the given month |
Year | ||
/L/e/a/p /Y/e/a/r: L | Leap Year: 0 | //L --Whether it's a leap year |
d m o | 15 12 2010 | //o--ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) |
d m Y | 15 12 2010 | //Y--A full numeric representation of a year, 4 digits |
d m y | 15 12 10 | //y--A two digit representation of a year |
Time | ||
D M Y a | Wed Dec 2010 am | //a --Lowercase Ante meridiem and Post meridiem |
D M Y A | Wed Dec 2010 AM | //A --Uppercase Ante meridiem and Post meridiem |
D M Y B | Wed Dec 2010 349 | //B --Swatch Internet time |
D M Y g | Wed Dec 2010 8 | //g --12-hour format of an hour without leading zeros |
D M Y G | Wed Dec 2010 8 | //G --24-hour format of an hour without leading zeros |
D M Y h | Wed Dec 2010 08 | //h --12-hour format of an hour with leading zeros |
D M Y H | Wed Dec 2010 08 | //H --24-hour format of an hour with leading zeros |
D M Y i | Wed Dec 2010 23 | //i --Minutes with leading zeros |
D M Y s | Wed Dec 2010 02 | //s --Seconds, with leading zeros |
D M Y u | Wed Dec 2010 000000 | //u --Microseconds (added in PHP 5.2.2) |
D M Y g:i:s | Wed Dec 2010 8:23:02 | show the time |
Timezone | ||
e | Europe/Paris | //e --Timezone identifier (added in PHP 5.1.0) |
/i/n /d/a/y/l/i/g/h/t I | in daylight 0 | //I (capital i) --Whether or not the date is in daylight saving time |
O | +0100 | //O -- Difference to Greenwich time (GMT) in hours |
P | +01:00 | //P -- Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) |
T | CET | //T --Timezone abbreviation |
Z | 3600 | //Z --Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. |
Full Date/Time | ||
c | 2010-12-15T08:23:02+01:00 | //c --ISO 8601 date (added in PHP 5) |
r | Wed, 15 Dec 2010 08:23:02 +0100 | //r --? RFC 2822 formatted date |
U | 1292397782 | //Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) |