今天我以一個簡單的利用date()函數(shù)輸出中文年月日時分秒引出關(guān)于大多數(shù)據(jù)用戶在使用php date()函數(shù)的一些問題與date()函數(shù)一些參數(shù)詳解。
當然了,PHP的DATE函數(shù)是不可能直接輸出中文的年月日的,但可以用下面這種方法自己寫一個函數(shù),實例代碼如下:
- function today(){
- date_default_timezone_set ("Asia/Chongqing");
- $a=date("Y");
- $b=date("m");
- $c=date("d");
- $d=date("G");
- $e=date("i");
- $f=date("s");
- return $a.'年'.$b.'月'.$c.'日'.$d.'時'.$e.'分'.$f.'秒';}
如果出現(xiàn)echo date("Y-m-d");
警號:
Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in G:sourcetestceshimktime.php on line 4
網(wǎng)上查了下資料才知道原因,下面給大家分享下,在用PHP5.3以上的PHP版本時,只要是涉及時間的會報一個
"PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in
以下是三種方法(任選一種都行):
一、在頁頭使用date_default_timezone_set()設(shè)置 date_default_timezone_set('PRC'); //東八時區(qū) echo date('Y-m-d H:i:s');
二、在頁頭使用 ini_set('date.timezone','Asia/Shanghai');
三、修改php.ini。打開php5.ini查找date.timezone 去掉前面的分號修改成為:date.timezone =PRC
重啟http服務(wù)(如apache2或iis等)即可。
關(guān)于date函數(shù)參數(shù):
d 月份中的第幾天,有前導(dǎo)零的 2 位數(shù)字 01 到 31
D 星期中的第幾天,文本表示,3 個字母 Mon 到 Sun
j 月份中的第幾天,沒有前導(dǎo)零 1 到 31
l (“L”的小寫字母) 星期幾,完整的文本格式 Sunday 到 Saturday
N ISO-8601 格式數(shù)字表示的星期中的第幾天(PHP 5.1.0 新加) 1(星期一)到 7(星期天)
S 每月天數(shù)后面的英文后綴,2 個字符 st,nd,rd 或者 th??梢院?j 一起用
w 星期中的第幾天,數(shù)字表示 0(星期天)到 6(星期六)
z 年份中的第幾天 0 到 366
W ISO-8601 格式年份中的第幾周,每周從星期一開始(PHP 4.1.0 新加的) 42(當年的第 42 周)
F 月份,完整的文本格式,例如 January 或者 March January 到 December
m 數(shù)字表示的月份,有前導(dǎo)零 01 到 12
M 三個字母縮寫表示的月份 Jan 到 Dec
n 數(shù)字表示的月份,沒有前導(dǎo)零 1 到 12
t 給定月份所應(yīng)有的天數(shù) 28 到 31
L 是否為閏年 如果是閏年為 1,否則為 0
o ISO-8601 格式年份數(shù)字。
Y 4 位數(shù)字完整表示的年份 例如:1999 或 2003
y 2 位數(shù)字表示的年份 例如:99 或 03
a 小寫的上午和下午值 am 或 pm
A 大寫的上午和下午值 AM 或 PM
B Swatch Internet 標準時 000 到 999
g 小時,12 小時格式,沒有前導(dǎo)零 1 到 12
G 小時,24 小時格式,沒有前導(dǎo)零 0 到 23
h 小時,12 小時格式,有前導(dǎo)零 01 到 12
H 小時,24 小時格式,有前導(dǎo)零 00 到 23
i 有前導(dǎo)零的分鐘數(shù) 00 到 59>
s 秒數(shù),有前導(dǎo)零 00 到 59>
e 時區(qū)標識(PHP 5.1.0 新加) 例如:UTC,GMT,Atlantic/Azores
I 是否為夏令時 如果是夏令時為 1,否則為 0
O 與格林威治時間相差的小時數(shù) 例如:+0200
P 與格林威治時間(GMT)的差別,小時和分鐘之間有冒號分隔 例如:+02:00
T 本機所在的時區(qū)
Z 時差偏移量的秒數(shù)。UTC 西邊的時區(qū)偏移量總是負的,UTC 東邊是正的。 -43200 到 43200
c ISO 8601 格式的日期(PHP 5 新加) 2004-02-12T15:19:21+00:00
r RFC 822 格式的日期 例如:Thu, 21 Dec 2000 16:01:07 +0200
U 從 Unix 紀元(January 1 1970 00:00:00 GMT)開始至今的秒數(shù) time()獲得時間戳
php的date()函數(shù)十分強大,通過上面這些參數(shù)可以實現(xiàn)很多日期的操作,比如說獲取上面我標記的紅色參數(shù)說明來實現(xiàn)今天是星期幾操作!
新聞熱點
疑難解答