欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 获取时间的方法

程序员文章站 2022-08-11 11:16:49
php 获取时间的方法 $year = date("Y");// 年 2018 如果“Y” 小写,输出是年份简写,如:2018年,...

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 的意思:公历;阳历