时间戳格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//获取今日开始时间戳和结束时间戳
$beginToday = mktime (0,0,0, date ( 'm' ), date ( 'd' ), date ( 'Y' ));
$endToday = mktime (0,0,0, date ( 'm' ), date ( 'd' )+1, date ( 'Y' ))-1;
//获取昨日起始时间戳和结束时间戳
$beginYesterday = mktime (0,0,0, date ( 'm' ), date ( 'd' )-1, date ( 'Y' ));
$endYesterday = mktime (0,0,0, date ( 'm' ), date ( 'd' ), date ( 'Y' ))-1;
//获取本周起始时间戳和结束时间戳
$beginThisweek = mktime (0,0,0, date ( 'm' ), date ( 'd' )- date ( 'w' )+1, date ( 'y' ));
$endThisweek =time();
//获取上周起始时间戳和结束时间戳
$beginLastweek = mktime (0,0,0, date ( 'm' ), date ( 'd' )- date ( 'w' )+1-7, date ( 'Y' ));
$endLastweek = mktime (23,59,59, date ( 'm' ), date ( 'd' )- date ( 'w' )+7-7, date ( 'Y' ));
//获取本月起始时间戳和结束时间戳
$beginThismonth = mktime (0,0,0, date ( 'm' ),1, date ( 'Y' ));
$endThismonth = mktime (23,59,59, date ( 'm' ), date ( 't' ), date ( 'Y' ));
//上个月的起始时间:
$begin_time = strtotime ( date ( 'Y-m-01 00:00:00' , strtotime ( '-1 month' )));
$end_time = strtotime ( date ( "Y-m-d 23:59:59" , strtotime (- date ( 'd' ). 'day' )));
$begin_year = strtotime ( date ( "Y" ,time()). "-1" . "-1" ); //本年开始
$end_year = strtotime ( date ( "Y" ,time()). "-12" . "-31" ); //本年结束
//现在的时间到第二天凌晨相差的时间戳
$time = ( strtotime ( date ( 'Y-m-d' ))+3600*24) - time() ;
|
日期格式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//上周起始时间
echo date ( "Y-m-d H:i:s" , mktime (0, 0 , 0, date ( "m" ), date ( "d" )- date ( "w" )+1-7, date ( "Y" )));
echo date ( "Y-m-d H:i:s" , mktime (23,59,59, date ( "m" ), date ( "d" )- date ( "w" )+7-7, date ( "Y" )));
//本周起始时间
echo date ( "Y-m-d H:i:s" , mktime (0, 0 , 0, date ( "m" ), date ( "d" )- date ( "w" )+1, date ( "Y" )));
echo date ( "Y-m-d H:i:s" , mktime (23,59,59, date ( "m" ), date ( "d" )- date ( "w" )+7, date ( "Y" )));
//上月起始时间
echo date ( "Y-m-d H:i:s" , mktime (0, 0 , 0, date ( "m" )-1,1, date ( "Y" )));
echo date ( "Y-m-d H:i:s" , mktime (23,59,59, date ( "m" ) ,0, date ( "Y" )));
//本月起始时间
echo date ( "Y-m-d H:i:s" , mktime (0, 0 , 0, date ( "m" ),1, date ( "Y" )));
echo date ( "Y-m-d H:i:s" , mktime (23,59,59, date ( "m" ), date ( "t" ), date ( "Y" )));
//本年起始时间
echo date (‘Y-01-01');
echo date (‘Y-12-31');
|
获取当月的最后一天和第一天
1
2
3
4
5
6
7
|
$BeginDate = date ( 'Y-m-01' , strtotime ( date ( "Y-m-d" )));
echo $BeginDate ;
echo "
";
echo date ( 'Y-m-d' , strtotime ( "$BeginDate +1 month -1 day" ));
echo "
";
|
获取当天年份,月份,以及当月天数
1
2
3
4
5
6
|
echo " 本月共有:" . date ( "t" ). "天" ;
echo " 当前年份" . date ( 'Y' );
echo " 当前月份" . date ( 'm' );
echo " 当前几号" . date ( 'd' );
echo "
";
|
获取指定月份的天数或者最后一天
1
2
3
4
5
6
7
8
9
10
|
$days = cal_days_in_month(CAL_GREGORIAN, 4, 2011);
echo "返回2011-4的天数 " . $days ."
";
$days = date ( 't' , strtotime ( "2011-4-1" ));
echo "返回2011-4的天数 " . $days ."
";
$days = date ( "t" );
echo "当前月的天数 " . $days ."
";
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获取本年、本月、本周时间戳和日期格式的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
原文链接:https://www.cnblogs.com/lazb/p/12209314.html