PHP获取今天、明天、上周、本周、上月、本月、本季度、上季度时间段方法
strtotime()函数
1 2 3 | //date('n') 第几个月 //date("w") 本周周几 //date("t") 本月天数 |
时间段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | //当前时间 echo date ( "Y-m-d H:i:s" ,time()); //明天此时时间 echo date ( "Y-m-d H:i:s" , strtotime ( "+1 day" )); //指定时间 echo date ( "Y-m-d H:i:s" , strtotime ( "-1 day" )); //下星期时间 echo date ( "Y-m-d H:i:s" , strtotime ( "+1 week" )) //上个星期此时时间 echo date ( "Y-m-d H:i:s" , strtotime ( "-1 week" )); //下星期几时间 echo date ( "Y-m-d H:i:s" , strtotime ( "next Thursday" )); //指定时间 echo date ( "Y-m-d H:i:s" , strtotime ( "last Thursday" )); |
以上PHP时间戳函数示例可知,strtotime能将任何英文文本的日期时间描述解析为Unix时间戳,我们结合mktime()或date()格式化日期时间获取指定的时间戳,实现所需要的日期时间
今天
1 2 | $startDate = mktime (0,0,0, date ( "m" ), date ( "d" ), date ( "Y" )); $endDate = mktime (0,0,0, date ( 'm' ), date ( 'd' )+1, date ( 'Y' ))-1; |
昨天
1 2 | $startDate = mktime (0,0,0, date ( "m" ), date ( "d" )-1, date ( "Y" )); $endDate = mktime (0,0,0, date ( "m" ), date ( "d" ), date ( "Y" ))-1; |
明天
1 2 | $startDate = mktime (0,0,0, date ( "m" ), date ( "d" )+1, date ( "Y" )); $endDate = mktime (0,0,0, date ( "m" ), date ( "d" )+2, date ( "Y" ))-1; |
本周
1 2 3 | $w = date ( "w" ); $startDate = mktime (0,0,0, date ( "m" ), date ( "d" )- $w +1, date ( "Y" )); $endDate = mktime (23,59,59, date ( 'm' ), date ( 'd' )- date ( 'w' )+7, date ( 'Y' )); |
上周
1 2 | $startDate = mktime (0,0,0, date ( 'm' ), date ( 'd' )- date ( 'w' )+1-7, date ( 'Y' )); $endDate = mktime (23,59,59, date ( 'm' ), date ( 'd' )- date ( 'w' )+7-7, date ( 'Y' )); |
下周
1 2 | $startDate = mktime (0,0,0, date ( 'm' ), date ( 'd' )- date ( 'w' )+7+1, date ( 'Y' )); $endDate = mktime (23,59,59, date ( 'm' ), date ( 'd' )- date ( 'w' )+7+7, date ( 'Y' )); |
本月
1 2 | $startDate = mktime (0,0,0, date ( "m" ),1, date ( "Y" )); $endDate = mktime (23,59,59, date ( 'm' ), date ( 't' ), date ( 'Y' ))-1; |
上月
1 2 | $startDate = mktime (0,0,0, date ( "m" )-1,1, date ( "Y" )); $endDate = mktime (0,0,0, date ( "m" ),1, date ( "Y" ))-1; |
下月
1 2 | $startDate = mktime (0,0,0, date ( "m" )+1,1, date ( "Y" )); $endDate = mktime (0,0,0, date ( "m" )+2,1, date ( "Y" ))-1; |
本季度
1 2 3 4 5 6 7 8 9 10 11 12 | $getMonthDays = date ( "t" , mktime (0, 0 , 0, date ( 'n' )+( date ( 'n' )-1)%3,1, date ( "Y" ))); //本季度未最后一月天数 echo '<br>本季度:<br>' ; echo date ( 'Y-m-d H:i:s' , mktime (0, 0, 0, date ( 'n' )-( date ( 'n' )-1)%3,1, date ( 'Y' ))), "\n" ; echo date ( 'Y-m-d H:i:s' , mktime (23,59,59, date ( 'n' )+( date ( 'n' )-1)%3, $getMonthDays , date ( 'Y' ))), "\n" ; $season = ceil (( date ( 'n' ))/3); //当月是第几季度 echo '<br>本季度起始时间:<br>' ; echo date ( 'Y-m-d H:i:s' , mktime (0, 0, 0, $season *3-3+1,1, date ( 'Y' ))), "\n" ; echo date ( 'Y-m-d H:i:s' , mktime (23,59,59, $season *3, date ( 't' , mktime (0, 0 , 0, $season *3,1, date ( "Y" ))), date ( 'Y' ))), "\n" ; $season = ceil (( date ( 'n' ))/3)-1; //上季度是第几季度 echo '<br>上季度起始时间:<br>' ; echo date ( 'Y-m-d H:i:s' , mktime (0, 0, 0, $season *3-3+1,1, date ( 'Y' ))), "\n" ; echo date ( 'Y-m-d H:i:s' , mktime (23,59,59, $season *3, date ( 't' , mktime (0, 0 , 0, $season *3,1, date ( "Y" ))), date ( 'Y' ))), "\n" ; |
今年
1 2 | $startDate = mktime (0,0,0,1,1, date ( "Y" )); $endDate = mktime (0,0,0,1,1, date ( "Y" )+1)-1; |
结果如:
1483200000
1514735999
格式化:
2017-01-01 00:00:00
2017-12-31 23:59:59
语法
mktime(hour,minute,second,month,day,year,is_dst)
参数 描述
hour 可选。规定小时。
minute 可选。规定分钟。
second 可选。规定秒。
month 可选。规定用数字表示的月。
day 可选。规定天。
year 可选。规定年。在某些系统上,合法值介于 1901 - 2038 之间。不过在 PHP 5 中已经不存在这个限制了。
is_dst
可选。如果时间在日光节约时间(DST)期间,则设置为1,否则设置为0,若未知,则设置为-1。
自 5.1.0 起,is_dst 参数被废弃。因此应该使用新的时区处理特性。