共計 881 個字符,預計需要花費 3 分鐘才能閱讀完成。
php 中日期相關的函數有 date_create、date_diff、strtotime、microtime 常見的幾種
1.date_create 函數
date_create 函數作用:
php 中 date_create 函數的作用是用于返回一個新的 DateTime 對象。
date_create 函數語法:
date_create(time,timezone);
參數:
time:規定日期時間字符串。
timezone:規定 time 的時區。
date_create 函數使用方法:
例:創建一個 DateTime 對象,并格式化輸出
$date=date_create("2021-03-23");
echo date_format($date,"Y/m/d");
2.date_diff 函數
date_diff 函數作用:
php 中 date_diff 函數的作用是用于返回兩個 DateTime 對象間的差值。
date_diff 函數語法:
date_diff(datetime1,datetime2)
date_diff 函數使用方法:
例:計算時間 2020-03-23 至 2021-03-23 的差值
$date1=date_create("2020-03-23");
$date2=date_create("2021-03-23");
$diff=date_diff($date1,$date2);
3.strtotime 函數
strtotime 函數作用:
php 中 strtotime 函數的作用是將字符串的日期時間解析為 Unix 時間戳。
strtotime 函數語法:
strtotime(time,now);
strtotime 函數使用方法:
echo(strtotime("next Monday") ;
echo(strtotime("last Sunday"));
4.microtime 函數
microtime 函數作用:
php 中 microtime 函數的作用是返回當前 Unix 時間戳的微秒數。
microtime 函數語法:
microtime();
microtime 函數使用方法:
echo(microtime());
丸趣 TV 網 – 提供最優質的資源集合!