本篇介紹了php擷取時間的方法
$year = date("Y");// 年 2018 如果“Y” 小寫,輸出是年份簡寫,如:2018年,輸出是:18$month =date("m");// 月 2 如果“M” 大寫,輸出是英文月份,小寫是數字$day =date("d");// 日$week =date("l");// 星期幾$last_month_days = cal_days_in_month(CAL_GREGORIAN, $month-1,$year);// 上一月的天數 $month = date("m")$now_month_days = cal_days_in_month(CAL_GREGORIAN, $month,$year);// 當年月的天數; $month = date("m")cal_days_in_month 的公用:返回某個曆法中某年中某月的天數$next_month_days=cal_days_in_month(CAL_GREGORIAN,$month+1,$year);if($year%4==0 && $now_month_days==28){$now_month_days = $now_month_days+1;// 判斷當前月是不閏年,是的話 2月加一天,即29天}$week_date = date("w",strtotime("2018-2-5")); // 字串指定日期獲得星期幾$week_date = date("w",strtotime(date("Y-m-d))); // 變數指定日期獲得星期幾$week_date = date("w",strtotime($year."-".$month."-"."1"));//拼接字串指定每月 1 號期,獲得星期幾 cal_days_in_month 的公用:返回某個曆法中某年中某月的天數
參數介紹:
cal_days_in_month(par1,par2,$par3);
$par1 :用來計算的某種曆法,PHP Calendar 常量
par2:參數par1選中曆法中的某個月
$par3: 選中曆法中的某一年
傳回值:
$par1 選中曆法的某年某月的天數
PHP Calendar 常量:日曆擴充包含了簡化不同日曆格式間轉換的函數。
為了讓這些函數能夠工作,您必須通過 –enable-calendar 編譯 PHP。window下,已經整合了對日曆擴充的支援
案例中的常量CAL_GREGORIAN,是PHP預定義的常量,就像PHP_OS等常量
gregorian 的意思:西曆;陽曆
本篇介紹了php擷取時間的方法,更多相關內容請關注php中文網。