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 $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($current_time){
- $current_time = strtotime(date($current_time ',$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";
-
複製程式碼
|