php date mktime strtotime 获取时间方法

WBOY
Lepaskan: 2016-07-25 08:45:57
asal
1145 orang telah melayarinya
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 $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));
  49. }
  50. return $week_arr;
  51. }
  52. /**
  53. * 获取本周的开始时间和结束时间
  54. */
  55. function getWeek_SdateAndEdate($current_time){
  56. $current_time = strtotime(date('Y-m-d',$current_time));
  57. $return_arr['sdate'] = date('Y-m-d', $current_time-86400*(date('N',$current_time) - 1));
  58. $return_arr['edate'] = date('Y-m-d', $current_time+86400*(7- date('N',$current_time)));
  59. return $return_arr;
  60. }
  61. //查询当天、前3天、本周、本月、本年的时间
  62. echo date("y-m-d",mktime(0, 0 , 0,date("m"),date("d"),date("Y"))-86400*3); //当前日期往前推3天
  63. //thinkphp 里面查询当天、本周、本月、本年的时间
  64. $time=I('get.time');
  65. switch ($time) {
  66. case 'day':
  67. $startTime=date("Y-m-d");
  68. $endTime=date("Y-m-d");
  69. break;
  70. case 'week':
  71. $startTime=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
  72. $endTime =date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
  73. break;
  74. case 'month':
  75. $startTime = date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
  76. $endTime=date("Y-m-d",mktime(23,59,59,date("m"),date("t"),date("Y")));
  77. break;
  78. case 'year':
  79. $startTime=date("Y").'-01-01';
  80. $endTime=date("Y").'-12-31';
  81. break;
  82. default:
  83. break;
  84. }
  85. //其他方式获取PHP获取上周、本周、上月、本月、本季度、上季度时间方法大全
  86. echo date("Ymd",strtotime("now")), "\n";
  87. echo date("Ymd",strtotime("-1 week Monday")), "\n";
  88. echo date("Ymd",strtotime("-1 week Sunday")), "\n";
  89. echo date("Ymd",strtotime("+0 week Monday")), "\n";
  90. echo date("Ymd",strtotime("+0 week Sunday")), "\n";
  91. echo "*********第几个月:";
  92. echo date('n');
  93. echo "*********本周周几:";
  94. echo date("w");
  95. echo "*********本月天数:";
  96. echo date("t");
  97. echo "*********";
  98. echo '
    上周起始时间:
    ';
  99. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))),"\n";
  100. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))),"\n";
  101. echo '
    本周起始时间:
    ';
  102. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"))),"\n";
  103. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))),"\n";
  104. echo '
    上月起始时间:
    ';
  105. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))),"\n";
  106. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))),"\n";
  107. echo '
    本月起始时间:
    ';
  108. echo date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y"))),"\n";
  109. echo date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))),"\n";
  110. $season = ceil((date('n'))/3);//当月是第几季度
  111. echo '
    本季度起始时间:
    ';
  112. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
  113. 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";
  114. $season = ceil((date('n'))/3)-1;//上季度是第几季度
  115. echo '
    上季度起始时间:
    ';
  116. echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
  117. 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


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!