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

PHP获取本周、上周、本月、上月、本季度、上季度等各类具体时间

程序员文章站 2022-05-04 14:26:01
1.获取本周开始和结束的日期: echo date("Y-m-d H:i:s", mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))); //2020-03-09 00:00:00 echo date("Y-m-d H:i:s", mkti ......

1.获取本周开始和结束的日期:

echo date("y-m-d h:i:s", mktime(0,0,0,date("m"),date("d")-date("w")+1,date("y")));    //2020-03-09 00:00:00
echo date("y-m-d h:i:s", mktime(23,59,59,date("m"),date("d")-date("w")+7,date("y"))); //2020-03-15 23:59:59

2.获取上周开始和结束的日期:

echo date('y-m-d h:i:s', mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('y')));    //2020-03-02 00:00:00
echo date('y-m-d h:i:s', mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('y'))); //2020-03-08 23:59:59

3.获取本月开始和结束的日期:

echo date('y-m-d h:i:s', mktime(0,0,0,date('m'),1,date('y')));        //2020-03-01 00:00:00
echo date('y-m-d h:i:s', mktime(23,59,59,date('m'),date('t'),date('y'))); //2020-03-31 23:59:59

4.获取上月开始和结束的日期:

echo date("y-m-d h:i:s", mktime(0,0,0,date("m")-1,1,date("y")));    //2020-02-01 00:00:00
echo date("y-m-d h:i:s", mktime(23,59,59,date("m") ,0,date("y")));  //2020-02-29 23:59:59

5.获取本季度开始和结束的日期:

$season = ceil((date('n'))/3);
echo date('y-m-d h:i:s', mktime(0,0,0,$season*3-3+1,1,date('y')));                           //2020-01-01 00:00:00
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'))); //2020-03-31 23:59:59

6.获取上季度开始和结束的日期:

$season = ceil((date('n'))/3)-1;//上季度是第几季度
echo date('y-m-d h:i:s', mktime(0,0,0,$season*3-3+1,1,date('y')));                          //2019-10-01 00:00:00
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')));//2019-12-31 23:59:59

7.获取本月是第几月:

echo date('n'); //3

8.获取今天是周几:

echo date("w"); //4

9.获取本月总天数:

echo date("t"); //31