当前位置:Gxlcms > PHP教程 > phpdatemktimestrtotime获取时间方法

phpdatemktimestrtotime获取时间方法

时间:2021-07-01 10:21:17 帮助过:45人阅读

php date mktime strtotime 获取时间方法
  1. /**
  2. * 获得系统年份数组
  3. */
  4. function getSystemYearArr(){
  5. $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');
  6. return $year_arr;
  7. }
  8. /**
  9. * 获得系统月份数组
  10. */
  11. function getSystemMonthArr(){
  12. $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');
  13. return $month_arr;
  14. }
  15. /**
  16. * 获得系统周数组
  17. */
  18. function getSystemWeekArr(){
  19. $week_arr = array('1'=>'周一','2'=>'周二','3'=>'周三','4'=>'周四','5'=>'周五','6'=>'周六','7'=>'周日');
  20. return $week_arr;
  21. }
  22. /**
  23. * 获取某月的最后一天
  24. */
  25. function getMonthLastDay($year, $month){
  26. $t = mktime(0, 0, 0, $month + 1, 1, $year);
  27. $t = $t - 60 * 60 * 24;
  28. return $t;
  29. }
  30. /**
  31. * 获得系统某月的周数组,第一周不足的需要补足
  32. */
  33. function getMonthWeekArr($current_year, $current_month){
  34. //该月第一天
  35. $firstday = strtotime($current_year.'-'.$current_month.'-01');
  36. //该月的第一周有几天
  37. $firstweekday = (7 - date('N',$firstday) +1);
  38. //计算该月第一个周一的时间
  39. $starttime = $firstday-3600*24*(7-$firstweekday);
  40. //该月的最后一天
  41. $lastday = strtotime($current_year.'-'.$current_month.'-01'." +1 month -1 day");
  42. //该月的最后一周有几天
  43. $lastweekday = date('N',$lastday);
  44. //该月的最后一个周末的时间
  45. $endtime = $lastday-3600*24*$lastweekday;
  46. $step = 3600*24*7;//步长值
  47. $week_arr = array();
  48. for ($i=$starttime; $i<$endtime; $i= $i+3600*24*7){
  49. $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));
  50. }
  51. return $week_arr;
  52. }
  53. /**
  54. * 获取本周的开始时间和结束时间
  55. */
  56. function getWeek_SdateAndEdate($current_time){
  57. $current_time = strtotime(date('Y-m-d',$current_time));
  58. $return_arr['sdate'] = date('Y-m-d', $current_time-86400*(date('N',$current_time) - 1));
  59. $return_arr['edate'] = date('Y-m-d', $current_time+86400*(7- date('N',$current_time)));
  60. return $return_arr;
  61. }
  62. //查询当天、前3天、本周、本月、本年的时间
  63. echo date("y-m-d",mktime(0, 0 , 0,date("m"),date("d"),date("Y"))-86400*3); //当前日期往前推3天
  64. //thinkphp 里面查询当天、本周、本月、本年的时间
  65. $time=I('get.time');
  66. switch ($time) {
  67. case 'day':
  68. $startTime=date("Y-m-d");
  69. $endTime=date("Y-m-d");
  70. break;
  71. case 'week':
  72. $startTime=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
  73. $endTime =date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
  74. break;
  75. case 'month':
  76. $startTime = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
  77. $endTime=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
  78. break;
  79. case 'year':
  80. $startTime=date("Y").'-01-01';
  81. $endTime=date("Y").'-12-31';
  82. break;
  83. default:
  84. break;
  85. }
  86. //其他方式获取PHP获取上周、本周、上月、本月、本季度、上季度时间方法大全
  87. echo date("Ymd",strtotime("now")), "\n";
  88. echo date("Ymd",strtotime("-1 week Monday")), "\n";
  89. echo date("Ymd",strtotime("-1 week Sunday")), "\n";
  90. echo date("Ymd",strtotime("+0 week Monday")), "\n";
  91. echo date("Ymd",strtotime("+0 week Sunday")), "\n";
  92. echo "*********第几个月:";
  93. echo date('n');
  94. echo "*********本周周几:";
  95. echo date("w");
  96. echo "*********本月天数:";
  97. echo date("t");
  98. echo "*********";
  99. echo '
    上周起始时间:
    ';
  100. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
  101. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
  102. echo '
    本周起始时间:
    ';
  103. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
  104. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";
  105. echo '
    上月起始时间:
    ';
  106. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
  107. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
  108. echo '
    本月起始时间:
    ';
  109. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
  110. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";
  111. $season = ceil((date('n'))/3);//当月是第几季度
  112. echo '
    本季度起始时间:
    ';
  113. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
  114. 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";
  115. $season = ceil((date('n'))/3)-1;//上季度是第几季度
  116. echo '
    上季度起始时间:
    ';
  117. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
  118. 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";
date, php, strtotime

人气教程排行