php date mktime strtotime 获取时间方法
- /**
- * 获得系统年份数组
- */
- function getSystemYearArr(){
- $year_arr = array('2010'=>'2010','2011'=>'2011','2012'=>'2012','2014'=>'2014','2014'=>'2014','2015'=>'2015','2016'=>'2016','2017'=>'2017','2018'=>'2018','2019'=>'2019','2020'=>'2020');
- return $year_arr;
- }
- /**
- * 获得系统月份数组
- */
- function getSystemMonthArr(){
- $month_arr = array('1'=>'01','2'=>'02','3'=>'03','4'=>'04','5'=>'05','6'=>'06','7'=>'07','8'=>'08','9'=>'09','10'=>'10','11'=>'11','12'=>'12');
- return $month_arr;
- }
- /**
- * 获得系统周数组
- */
- function getSystemWeekArr(){
- $week_arr = array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
- return $week_arr;
- }
- /**
- * 获取某月的最后一天
- */
- function getMonthLastDay($year, $month){
- $t = mktime(0, 0, 0, $month + 1, 1, $year);
- $t = $t - 60 * 60 * 24;
- return $t;
- }
- /**
- * 获得系统某月的周数组,第一周不足的需要补足
- */
- function getMonthWeekArr($current_year, $current_month){
- //该月第一天
- $firstday = strtotime($current_year.'-'.$current_month.'-01');
- //该月的第一周有几天
- $firstweekday = (7 - date('N',$firstday) +1);
- //计算该月第一个周一的时间
- $starttime = $firstday-3600*24*(7-$firstweekday);
- //该月的最后一天
- $lastday = strtotime($current_year.'-'.$current_month.'-01'." +1 month -1 day");
- //该月的最后一周有几天
- $lastweekday = date('N',$lastday);
- //该月的最后一个周末的时间
- $endtime = $lastday-3600*24*$lastweekday;
- $step = 3600*24*7;//步长值
- $week_arr = array();
- for ($i=$starttime; $i<$endtime; $i= $i+3600*24*7){
- $week_arr[] = array('key'=>date('Y-m-d',$i).'|'.date('Y-m-d',$i+3600*24*6), 'val'=>date('Y-m-d',$i).'~'.date('Y-m-d',$i+3600*24*6));
- }
- return $week_arr;
- }
- /**
- * 获取本周的开始时间和结束时间
- */
- function getWeek_SdateAndEdate($current_time){
- $current_time = strtotime(date('Y-m-d',$current_time));
- $return_arr['sdate'] = date('Y-m-d', $current_time-86400*(date('N',$current_time) - 1));
- $return_arr['edate'] = date('Y-m-d', $current_time+86400*(7- date('N',$current_time)));
- return $return_arr;
- }
- //查询当天、前3天、本周、本月、本年的时间
- echo date("y-m-d",mktime(0, 0 , 0,date("m"),date("d"),date("Y"))-86400*3); //当前日期往前推3天
- //thinkphp 里面查询当天、本周、本月、本年的时间
- $time=I('get.time');
- switch ($time) {
- case 'day':
- $startTime=date("Y-m-d");
- $endTime=date("Y-m-d");
- break;
- case 'week':
- $startTime=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
- $endTime =date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
- break;
- case 'month':
- $startTime = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
- $endTime=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
- break;
- case 'year':
- $startTime=date("Y").'-01-01';
- $endTime=date("Y").'-12-31';
- break;
- default:
- break;
- }
- //其他方式获取PHP获取上周、本周、上月、本月、本季度、上季度时间方法大全
- echo date("Ymd",strtotime("now")), "\n";
- echo date("Ymd",strtotime("-1 week Monday")), "\n";
- echo date("Ymd",strtotime("-1 week Sunday")), "\n";
- echo date("Ymd",strtotime("+0 week Monday")), "\n";
- echo date("Ymd",strtotime("+0 week Sunday")), "\n";
-
- echo "*********第几个月:";
- echo date('n');
- echo "*********本周周几:";
- echo date("w");
- echo "*********本月天数:";
- echo date("t");
- echo "*********";
-
- echo '
上周起始时间: '; - echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
- echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
- echo '
本周起始时间: '; - echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
- echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";
-
- echo '
上月起始时间: '; - echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
- echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
- echo '
本月起始时间: '; - echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
- echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";
-
- $season = ceil((date('n'))/3);//当月是第几季度
- echo '
本季度起始时间: '; - 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 '
上季度起始时间: '; - 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";
复制代码 |